Formation of bounding volume hierarchies

ABSTRACT

A method in a graphics processor searches for a candidate reinsertion for each of a plurality of input nodes in a current bounding volume hierarchy (BVH), which would move the respective input node from an old parent to a new parent, and which would reduce the expected computational cost of searching the BVH for a ray intersection; and updates the current BVH with one or more selected reinsertions selected from among the candidates. In the search for candidate reinsertions, either or both of: a) the new parent is limited to being related to the old parent by an ancestor at no more than a predetermined number of hierarchal levels above the old parent, and/or b) the input nodes are limited to being at or above a hierarchical level a predetermined number of hierarchical levels below the root node.

CLAIM OF PRIORITY

This application claims priority under 35 U.S.C. 119 from United Kingdompatent application Nos. GB2204657.7, GB2204658.5 and GB2204664.3, filedon 31 Mar. 2022; and GB2301775.9, filed 8 Feb. 2023, which are hereinincorporated by reference in their entirety.

BACKGROUND

A processor is a device for executing a set of machine code instructionsincluding various general-purpose instructions such as add, multiply,etc. An application-specific processor, such as a graphics processingunit (GPU), can be tailored to a specific application by including oneor more dedicated hardware modules for performing one or more specifictypes of operation in fixed-function hardware circuitry. Such hardwaremay be invoked for example by one or more specialised instruction typesin the instruction set of the processor, or by writing to dedicatedregisters or to a buffer in a dedicated region of memory, or such like,depending on the design of the processor.

Ray tracing is one task which a graphics processor may be used toperform, either in software or dedicated hardware, or more typically acombination. Ray tracing refers to a graphics processing technique forgenerating an image by tracing a path of light through a modelledenvironment and simulating the effects of its encounters with objectsalong the way. Modelled rays of light are traced from a modelled sourceto a modelled view point (forward ray tracing) or vice versa backwardsfrom the modelled view point to the modelled source (i.e. reverse raytracing, which is typically more efficient as forward ray tracing oftenresults in processing rays whose trajectory ultimately never hits theviewpoint). A ray may be described by coordinates of an origin of theray, a vector specifying the direction of the ray, a maximum and minimumextent of the ray along that vector, and optionally a ray colour. Raytracing begins by casting rays out into the modelled environment, fromeach pixel in the image in the case of reverse ray tracing. Objects withwhich rays may interact in the modelled environment are divided intogeometric primitives, e.g. triangular facets. For each ray, the raytracing comprises finding the closest geometric primitive (if any) withwhich the ray interacts. In some graphics processors this search isperformed in fixed function hardware. When an incident ray intersects itcan then either terminate, reflect or refract. A reflection orrefraction introduces one or more secondary rays with a new directionrelative to the incident ray, which is terminated (i.e. the reflected orrefracted ray is modelled as a new ray). The secondary rays may alsoaccumulate a new value (colour) relative to the incident ray.

Ray tracing may be performed in software using general-purposeinstructions, or in dedicated hardware, or in a combination of these.For example in one GPU design, driver software run on the GPU forms a“bounding volume hierarchy” (BVH, to be discussed in more detailshortly) which is a data structure that divides a modelled environmentinto hierarchical regions for search purposes. The driver softwarewrites this data structure to a dedicated buffer in memory. A dedicated“traversal unit” implemented in hardware is arranged to then use the BVHto detect whether various modelled rays (originating from theapplication software and written to another dedicated region of memorycalled the ray buffer) would intersect with geometric primitives in themodelled environment.

Determining the effect of an interaction of a ray with a geometricprimitive is typically solved analytically in software. The program thatdoes this is called a shader program. There are typically differentshader programs that are run to handle different interaction scenarios.

For example, the different shader programs could comprise: a miss shaderprogram, a closest-hit shader program, an any-hit shader program, and anintersection shader program. The miss shader program is run when raydoes not hit anything. The closest-hit shader program is run when a rayhits a geometry where it is known that this hit is going to be kept andthe program is required to calculate the effects of light at the hitpoint. The any-hit shader program is run when a ray hits a geometry butthe program is also required to decide whether to keep the hit or not.The closest-hit shader program will be run afterwards if the hit iskept. The intersection shader program is run when a ray hits a box withuser-defined geometry in it and the program is required to procedurallygenerate or load up from memory that geometry, check which of thesegeometries are hit by the ray and then decide whether keep that hit. Theclosest-hit program will be run afterwards if the hit is kept. The aboveis a classification derived from the ray tracing API standards. In oneimplementation the any-hit and intersection shaders may be groupedtogether into a traverse shader.

More generally, operations done to support ray-tracing may comprise: raygeneration (spawn a ray), ray intersection (test a ray against aprimitive or a box containing primitives), ray traversal (search a treeof aforementioned boxes or other such scene acceleration structure, andschedule intersections along the walk of the tree).

A bounding volume hierarchy (BVH) is a type of data structure that isused in ray traversal. The data structure of the BVH takes the form of atree structure, in which nodes represent regions of space (typicallyboxes) in a modelled environment, and an edge from parent node to childnode represents that the region represented by the child node is nestedwithin the region represented by the parent. The nodes are thus arrangedin hierarchical levels from a root node down to a leaf node at thelowest level of each branch. The region of space represented by eachleaf node contains a respective one or more geometric primitives or atleast part of a geometric primitive. The BVH is used in the raytraversal mechanism to search for geometric primitives with which amodelled ray intersects. The search comprises first determining whichnode the ray would traverse at the first level down from the root, andthen determining which of that node's children the ray would intersect,and so forth, until the search ends with finding a leaf node traversedby the ray and determining whether the ray intersects with the primitiveor any of the primitives contained within that leaf node.

A simple way to form a BVH would simply be to divide the modelledenvironment in half, and then divide each half in half again, etc. So atthe first level below the root, the root has two child nodes eachrepresenting a different half of the space; then each of those nodes(unless it is a leaf) has its own two respective children (grandchildrenof the root) at the next level down, each dividing the bounding box ofits respective parent in half again, and so forth. At each level thespace could be divided in half e.g. by distance, or by number of voxels,or by median coordinate of primitive centroid (each “half” is sized suchthat it contains half the geometric primitives bounded by its parent,according to the position of the primitive centroids to a plane given bythe median coordinate on one axis).

However, the efficiency of searching the tree of the BVH depends on theway the modelled region of space is split between the different nodes ofthe tree. The simple approach described above will not necessarilyresult in the tree that, on average, incurs the lowest number ofcomputations to search in order to determine whether a ray intersectswith a primitive. There are known metrics for estimating the expectedcomputational cost of searching a BVH. One such metric is known as thesurface area heuristic (SAH), which measures the expected computationalcost of determining whether a random ray intersects with a primitive fora given BVH.

“Parallel Reinsertion for Bounding Volume Hierarchy Optimization”(Meister and Bittner, 2018) describes a method for optimizing the way inwhich a modelled space is split into different sized bounding boxes in abinary BVH. The method begins with some starting tree, then iterativelyconsiders different possible “reinsertions”. Each possible reinsertioncomprises taking an “input node” (one of the child nodes in the existingBVH) and considering moving it to a different part of the tree. Thebounds of the affected nodes will be shrunk or grown to accommodate themoving of the input node. If the input node is to be moved, then sincethe tree is a binary tree, the input node's old sibling will become a“singleton” or “only child”, making the old parent redundant. Hence thiswill mean removing the old parent and making the old sibling the childof the old parent's parent (the input node's old grandparent). And inthe place where the input node is to be re-inserted, a new node iscreated to parent the input node and its new sibling (the “targetnode”), maintaining the binary structure of the tree.

Meister's method goes through several iterations. Each iterationcomprises considering a plurality of possible candidate reinsertions,scoring each candidate reinsertion according to the SAH metric todetermine which would be beneficial (i.e. would reduce the expectedcomputational cost), and then selecting to execute at least some ofthese reinsertions (i.e. updating the BVH with the selectedreinsertions). Once the selected reinsertions of the current iterationhave been executed, the method loops around to another iteration whereit considers further possible reinsertions from the new starting pointof the newly updated BVH, and so forth.

SUMMARY

An issue with Meister's method is that the procedure of searching for,scoring and executing candidate reinsertions over multiple iterationsitself incurs processing resource. Therefore it would be beneficial totrade-off the complexity of the optimization search procedure itselfversus the likely savings in SAH (or such like) that are likely to beachieved.

Most beneficial SAH savings are likely to be found from candidatereinsertions closer to the top of the tree. Therefore by restricting themethod to input nodes above a certain search floor, this advantageouslyrestricts the processing burden of the method without undue loss to thelikely computational savings in ray traversal. Additionally, targetnodes for candidate reinsertions can also be restricted to be above thissearch floor. This also advantageously restricts the processing burdenof the search without undue loss to the likely savings.

Also, most beneficial SAH savings are likely to be found from candidatereinsertions within a certain distance of the input node. Therefore byrestricting the search to a certain search ceiling above the input node,this again advantageously restricts the processing burden of the searchwithout undue loss to the likely savings.

According to one aspect disclosed herein, there is provided a methodperformed by a graphics processor. The method comprises: obtaining astarting BVH, bounding volume hierarchy, being a data structurecomprising nodes representing different 3D regions of space in amodelled environment, the data structure comprising a tree in which thenodes are arranged hierarchically from a root node down to a pluralityof leaf nodes, wherein the region modelled by each leaf node encompassesat least one primitive or part of a primitive. The method furthercomprises performing one or more iterations, starting with a firstiteration that starts with the starting BVH as a current BVH. Eachiteration comprises: for each respective one of a plurality of inputnodes in the tree of the current BVH, searching for at least onerespective candidate reinsertion which would move the respective inputnode from an old parent to a new parent in the tree, and which comparedto the current BVH would reduce an expected computational cost ofsearching the tree to determine whether a modelled ray intersects withone of said primitives (according to a metric for estimating thecomputational cost); and performing at least a first update to updatethe current BVH with one or more selected reinsertions selected fromamong the candidate reinsertions. In the search for candidatereinsertions, either or both of: a) the new parent is limited to beingrelated to the old parent by an ancestor at no more than a predeterminednumber of hierarchal levels above the old parent in the tree of thecurrent BVH, and/or b) the input nodes are limited to being at or abovea hierarchical level in the tree of the current BVH a predeterminednumber of hierarchical levels below the root node.

In embodiments, the new parent may also be limited to being at or abovea hierarchical level in the tree of the current BVH a predeterminednumber of hierarchical levels below the root.

A given reinsertion may be defined as an operation which reinserts onlya single respective input node (no other nodes of the tree). Thus eachof one, more or all of the candidate reinsertions would reinsert onlyits one respective input node (and no other nodes of the tree). That isto say, each such reinsertion would move only its one respective inputnode to another branch of the tree (to a position alongside a targetnode, as opposed to, say, exchanging the input node with the targetnode).

This means that in each of one, some or all of the candidatereinsertions, the candidate reinsertion either i) increases a number ofchild nodes at the new parent (e.g. as in the case of a non-binaryreinsertion such as in FIG. 7 , discussed in more detail later); or ii)adds a new node to the data structure as the new parent (e.g. as in thecase of a binary reinsertion such as shown in FIG. 6 , also discussed inmore detail later).

Any or all of the selected reinsertions, selected for execution fromamong the candidates, may also be reinsertions meeting the abovedefinition.

In embodiments the method may comprise determining a starting scorebeing a score of the starting BVH according to said metric, wherein thefirst iteration starts with the starting score as a current score of thecurrent BVH. Each iteration (or at least some of them) may then compriseupdating the current score to account for the first update. The currentscore is a running value of the overall score of the current BVH. Thismay be used for example to determine whether the current BVH isconverging and therefore whether to perform a next one of saiditerations or instead terminate the method. As another example thecurrent overall score of the BVH may be used to determine a sparsityparameter for the search in the next iteration. Alternatively, it is notnecessary to update the score each iteration.

In embodiments, each update of the BVH comprises recalculating bounds ofbounding volumes modelled by any node having bounds affected by theupdate to the BVH. Alternatively, it is not necessary to update thebounding volumes every update.

Another issue with Meister's approach, which may be addressed inembodiments disclosed herein, is that it assumes the tree must always bekept as a binary tree with each reinsertion. It is recognized hereinthat this unduly restricts the opportunities for reducing the SAH (orsuch like). Sometimes, for a given input node, a candidate reinsertionthat does not assume a binary tree—such as by allowing three children ofa given parent—may give a better SAH saving than the possible binaryreinsertions of the same input node. It would therefore be beneficial toat least consider, at least in the scoring stage, one or more possiblereinsertions that do not retain a strict binary structure throughout thewhole tree.

Therefore in embodiments, in the searching step of at least one of theiterations, the one or more candidate reinsertions may be allowed toinclude at least one candidate reinsertion that would leave the oldparent with more than one child, and/or give the new parent more thantwo children. In embodiments, in the updating step of at least one ofthe iterations, at least one of the selected reinsertions may leave therespective old parent with more than one child, and/or gives therespective new parent more than two children.

In embodiments, the method allows for the evaluation of two reinsertiontypes: binary reinsertions (e.g. as seen in Meister's existing method)and non-binary reinsertions (inserting the node in an existing childlist).

In embodiments, the processor may comprise a buffer comprising aplurality of slots, each respective one of the nodes in the tree beingrepresented as an entry in a respective one of the plurality of slots.In such embodiments, when an old parent is removed in one of saiditerations, the respective slot is freed for storing a new entryrepresenting a newly created node created by a further one of theselected reinsertions in the same or a subsequent one of saiditerations. Said plurality of slots may be a fixed number of slots.

In further embodiments, the graphics processor may be configured to runa plurality of reinsertion processes, wherein each reinsertion processis configured for a respective input node, and each reinsertion processis identified by a respective process ID. Reinsertion processes or partsof such processes may be executed in parallel with one another. In suchembodiments, the reinsertion process for a respective input node maycomprise: performing the search for the candidate reinsertions of therespective input node; performing the scoring of the candidatereinsertions of the respective input node according to said metric; and,if any one of the candidate reinsertions of the respective input node isselected as a respective one of the selected reinsertions, performing anexecution of the selected reinsertion by performing the updating of thecurrent BVH with the respective selected reinsertion.

In embodiments, the further selected reinsertion (which uses the freedslot) may leave its respective old parent with at least two children,and create the newly created node as the respective new parent in orderto accommodate the input node as a sibling of another, target node inthe tree.

One issue with allowing non-binary reinsertions is that it may make theprogram non-deterministic. For instance, consider a scenario wheredifferent input nodes are processed by different threads which arescheduled in a round-robin manner or such like. If one thread can free aslot by removing a node from the tree, and another can re-use that slotto create a new node, then the exact behaviour of the program willdepend on which thread happens to get scheduled first. Using a strictbinary tree, this is not an issue because the removal of the input nodefrom its old position always removes exactly one node, and the insertionof the input node at its new position always creates exactly one newnode, so the process handling that reinsertion will just reuse the slotof the deleted node for the newly created node of the same reinsertion.However, if allowing non-binary behaviour, this will not necessarilyhappen for every possible reinsertion. Whilst not essential, it would bebeneficial for the program to allow a non-binary tree while stillremaining deterministic—i.e. if re-running the program (e.g. in testingor such like), one will always get exactly the same result for the sameframe or same point in time of the same scene.

Therefore in embodiments, in the execution of any of the selectedreinsertions, none of the freed slots may be allowed to be used to storean entry representing any node other than a newly created node createdby a process with the same process ID as that which freed the slot inthe same or a preceding iteration.

That is to say, the processor comprises a buffer comprising a pluralityof slots, each respective one of the nodes in the tree being representedas an entry in a respective one of the plurality of slots. Further,different input nodes may be processed by different reinsertionprocesses (e.g. threads or shader invocations), each reinsertion processbeing identified by a process ID, and at least some of the reinsertionprocesses being executed (at least in part) in parallel with oneanother. In this case when a first one of the reinsertion processesfrees a slot by removing a node from the tree, the method may compriseallowing only a reinsertion processes with the same process ID as thefirst reinsertion process to re-use the freed slot in order to create anew node. The reinsertion process that re-uses the slot may be the firstreinsertion process itself or another reinsertion process with the sameprocess ID. Preferably, no freed slot is allowed to be used to store anentry representing any node other than a newly created node created byone of the reinsertion processes with the same process ID as thereinsertion process which freed the respective slot.

Put another way, the processor comprises a buffer comprising a pluralityof slots, each respective one of the nodes in the tree being representedas an entry in a respective one of the plurality of slots; wherein thegraphics processor is configured to run a plurality of processesincluding at least some in parallel with one another, wherein eachprocess is configured to process a respective one or more of the inputnodes, each process being identified by a respective process ID; whereinin the updating step of at least one of the iterations (one or morefreeing iterations), an old parent is removed from the tree and the oldparent's respective slot is freed for storing a new entry representing anewly created node created by a further one of the selectedreinsertions; and wherein none of the freed slots (no freed slot, i.e.not any freed from any of said freeing iterations) is allowed to be usedto store an entry representing any node other than a newly created nodecreated by a process with the same process ID as that which freed theslot in the same or a preceding iteration.

This advantageously provides for a deterministic allocation scheme ofnew nodes within the operating range of the node buffer.

The method may be implemented by maintaining, for each process ID, alist of free slots that were freed by a process with the respectiveprocess ID; wherein each process is only allowed to search for acandidate reinsertion that would use a slot freed in a previousiteration if, on the list associated with the respective process ID,there is a free slot from the same iteration or a previous iteration.

In embodiments, the selected reinsertion processed by the firstreinsertion process may comprise a reinsertion that leaves therespective old parent with only one remaining child, such that therespective old parent is removed from the tree and the remaining childbecomes the child of the old parent's parent.

In embodiments, the further selected reinsertion may leave itsrespective old parent with at least two children, and create the newlycreated node as the respective new parent in order to accommodate theinput node as a sibling of another, target node in the tree.

In embodiments, in the execution of at least one of the selectedreinsertions, the freed slot may be used to store an entry representinga newly created node created by a process with the same process ID asthat which freed the slot in a preceding iteration.

In embodiments, the method may comprise one of said processes searchingfor a candidate reinsertion that would use a slot freed in a previousiteration, only if the slot was freed by a process in a previousiteration with the same respective process ID as said one of theprocesses.

In further, alternative or additional features of embodiments disclosedherein, the one or more selected reinsertions selected for inclusion inthe first update are selected according to a conflict check to determinewhether any group of the candidate reinsertions would affect a same partof the tree of the current BVH as one another, and if so selecting onlyone of the group to include in the first update. In some suchembodiments, at least one of the iterations may further comprise, afterthe first update, performing a second update within the same iterationto update the current BVH with another of said group.

Retrying previously conflicted reinsertions (resulting in multiple setsof reinsertions per iteration) advantageously allows for furtheropportunities for reducing the SAH (or other such metric) without theneed for repeating searches for candidate reinsertions. The method maycomprise validation of previously conflicted reinsertions beforeretrying them.

The method of any embodiment disclosed herein may be performed by logicimplemented in software stored in memory and arranged to run on one ormore; or implemented in fixed function hardware circuitry, or inconfigurable or reconfigurable hardware circuitry such as a PGA or FPGA;or any combination of hardware and software.

In any embodiment, the logic may be embodied in hardware on anintegrated circuit. There may be provided a method of manufacturing, atan integrated circuit manufacturing system, a processor comprising saidlogic. There may be provided an integrated circuit definition datasetthat, when processed in an integrated circuit manufacturing system,configures the system to manufacture the logic or processor. There maybe provided a non-transitory computer readable storage medium havingstored thereon a computer readable description of the logic or processorthat, when processed in an integrated circuit manufacturing system,causes the integrated circuit manufacturing system to manufacture anintegrated circuit embodying the logic or processor.

There may be provided an integrated circuit manufacturing systemcomprising: a non-transitory computer readable storage medium havingstored thereon a computer readable description of the logic orprocessor; a layout processing system configured to process the computerreadable description so as to generate a circuit layout description ofan integrated circuit embodying the logic or processor; and anintegrated circuit generation system configured to manufacture the logicor processor according to the circuit layout description.

There may be provided computer program code for performing any of themethods described herein. There may be provided non-transitory computerreadable storage medium having stored thereon computer readableinstructions that, when executed at a computer system, cause thecomputer system to perform any of the methods described herein.

The above features may be combined as appropriate, as would be apparentto a skilled person, and may be combined with any of the aspects of theexamples described herein.

This Summary is provided merely to illustrate some of the conceptsdisclosed herein and possible implementations thereof. Not everythingrecited in the Summary section is necessarily intended to be limiting onthe scope of the disclosure. Rather, the scope of the present disclosureis limited only by the claims.

BRIEF DESCRIPTION OF THE DRAWINGS

Examples will now be described in detail with reference to theaccompanying drawings in which:

FIG. 1 is a schematic block diagram of some logic for performing raytracing in software and/or hardware onboard a graphics processor,

FIG. 2 is a schematic illustration of a bounding volume hierarchy (BVH),

FIGS. 3 a and 3 b give a schematic illustration of some example leafnodes bounding geometric primitives in a BVH,

FIGS. 4 a and 4 b give a schematic illustration of some further examplesof leaf nodes bounding geometric primitives in a BVH,

FIG. 5 is a flow chart schematically illustrating a method of forming aBVH,

FIG. 6 gives a schematic illustration of an example of a method ofreinsertions in the formation of a BVH,

FIG. 7 gives a schematic illustration of an example of consideringpossible binary and non-binary reinsertions in accordance withembodiments disclosed herein,

FIG. 8 is a schematic block diagram of a computer system in which agraphics processing system is implemented,

FIG. 9 is a schematic block diagram of an integrated circuitmanufacturing system for generating an integrated circuit embodying agraphics processing system,

FIG. 10 gives a schematic illustration of an example of conflictingcandidate reinsertions whereby one of the reinsertions may be retried inaccordance with embodiments disclosed herein,

FIG. 11 gives a schematic illustration of another example of conflictingcandidate reinsertions whereby one of the reinsertions may be tried in asubsequent iteration in accordance with embodiments disclosed herein,

FIG. 12 gives a schematic illustration of limiting a search ceiling in asearch for reinsertions in accordance with embodiments disclosed herein,

FIG. 13 is a plot of simulated results of SAH reduction achieved byschemes that allow different numbers of conflict retries according toembodiments disclosed herein,

FIG. 14 a is a plot of simulated results of SAH reduction achieved byimposing a search floor in accordance with embodiments disclosed herein,and

FIG. 14 b is a plot of simulated results of SAH reduction achieved byimposing a search ceiling in accordance with embodiments disclosedherein.

The accompanying drawings illustrate various examples. The skilledperson will appreciate that the illustrated element boundaries (e.g.,boxes, groups of boxes, or other shapes) in the drawings represent oneexample of the boundaries. It may be that in some examples, one elementmay be designed as multiple elements or that multiple elements may bedesigned as one element. Common reference numerals are used throughoutthe figures, where appropriate, to indicate similar features.

DETAILED DESCRIPTION

The following description is presented by way of example to enable aperson skilled in the art to make and use the invention. The presentinvention is not limited to the embodiments described herein and variousmodifications to the disclosed embodiments will be apparent to thoseskilled in the art. Embodiments will now be described by way of exampleonly.

The present disclosure provides methods of optimizing bounding volumehierarchies (BVHs) for ray tracing. The quality of an existing hierarchyis improved in a post process that is parallelised for fast execution,in embodiments using compute shaders for execution on a GPU.

According to embodiments disclosed herein, a parallel reinsertion methodsuch as that of Meister may be extended by any one, more or all of thefollowing optimizations.

-   -   I. Optimisation of non-binary hierarchies. Non-binary BVHs can        improve tracing performance and reduce bandwidth, so being able        to optimise existing BVHs that have accounted for this, and        output BVHs with these benefits, is a useful trait.    -   II. Retries of conflicted reinsertions. Instead of discarding        proposed updates to the hierarchy due to conflicts with higher        scoring reinsertions, they can be reattempted afterwards. This        increases the number of useful updates to the BVH per search        (the most expensive phase of each iteration). Multiple sets of        reinsertions can be executed per iteration using these retries.    -   III. Only optimising the first 2^(k) nodes in the hierarchy (in        breadth first order), reducing computation time with minimal        impact to final hierarchy quality.    -   IV. Restricting the extents of searches for new positions        (‘targets’) in the hierarchy, reducing computation time with        minimal impact to final hierarchy quality.

BVH Overview

FIG. 1 schematically illustrates the logic 100 that may implement anyone or more of the disclosed methods in accordance with embodimentsdisclosed herein. The logic 100 comprises a bounding volume hierarchy(BVH) formation module 102, BVH storage 104, a ray traversal unit 106,and a ray buffer 108. Each of the BVH formation module 102 and raytraversal unit 106 is operatively coupled to the BVH storage 104. Thetraversal unit 106 is also operatively coupled to the ray buffer 108. Inoperation, the BVH formation module forms a BVH through an iterativeprocess to be discussed in more detail shortly, and stores the resultingBVH in the BVH storage 104. The modelled rays may originate fromapplication software run on the graphics processor or a host processor,and are stored in the ray buffer 108. The ray traversal unit 106 (whichcould also be referred to as the ray intersection unit or such like)reads the stored BVH from the BVH storage 104, and reads the rays fromthe ray buffer 108, and uses the BVH to determine whether each of themodelled rays intersects with one or more geometric primitives boundedby the leaves of the BVH.

The logic 100 may be implemented in software stored in one or morememory units and arranged to run on one or more execution units of agraphics processor. Alternatively the logic 100 may be implemented infixed-function hardware of the graphics processor, or in configurable orreconfigurable circuitry such as a programmable gate array (PGA) orfield programmable gate array (FPGA). As another alternative the logic100 may be implemented in a combination of hardware and software. In oneexample implementation, the BVH formation module 102 is implemented indriver software of the graphics processor, and the ray traversal unit106 is implemented as a fixed function hardware unit in the graphicsprocessor. The BVH storage 104 may be implemented in a dedicated memoryor dedicated region of memory, such as a RAM, or in dedicated registers.Similarly the ray buffer 108 may be implemented in a dedicated memory ordedicated region of memory, e.g. RAM, or in dedicated registers.

FIG. 2 illustrates, by way of example, the concept of a bounding volumehierarchy (BVH) 200. The BVH comprises a data structure arranged as atree, in which the nodes are arranged in hierarchical levels or layers,from a plurality of leaf nodes 206, through one or more levels ofinternal nodes (204), up to a root node (202). The root node 202 andeach internal node 204 (i.e. each node but the leaf nodes 206) are eachparents of a respective one or more child nodes, the respective childnodes being either other internal nodes 204 or leaf nodes 206. The rootnode 202 is only a parent, not a child; and the leaf nodes 206 are onlychildren, not parents. In a binary tree, the root node 202 and eachinternal node 204 (each node but the leaves) each has exactly twochildren.

Note: referring to the tree being arranged in level or layers hereindoes not imply all the leaf nodes 206 must be at the same depth in thetree. In practice BVHs will often be unbalanced and have leaves 206 atdifferent depths.

Each node 202, 204, 206 represents (models) a different respectivevolume of space—a so-called bounding volume, i.e. a 3D region ofspace—in a modelled environment. The volume represented by each leafnode 206 encompasses at least one respective geometric primitive or partof a respective geometric primitive. The root 202 typically representsthe whole environment. An edge from child to parent represents that thevolume of space represented by the child is contained (nested) withinthe region represented by the parent. Going down the tree from root toleaves, each internal node 204 represents a smaller region of spacenested within its parent, until the tree reaches the smallest level ofbounding volume at the leaf nodes.

A BVH provides a way of searching a volume of space in a modelledenvironment for intersections between a ray and a geometric primitive.Geometric primitives are geometric units (e.g. triangular facets) fromwhich larger objects may be formed within the modelled environment. Forexample an approximation of a curved surface may be made up of multiplesmaller triangular facets. Geometric primitives are sometimes justcalled primitives for short. It is the aim of ray traversal to determinewhich rays intersect with which primitives within the modelledenvironment.

Note: geometric primitives as referred to herein are not limited tobeing simple triangular or polygonal primitives, and the term may alsocover the possibility of procedural primitives. These areprogrammatically defined shapes, e.g. a mathematically defined sphere,with an associated bounding box, for which a shader may be used todetermine whether a ray hits the shape once it has determined whetherthe ray intersects the box.

According to the BVH, the 3D space of the modelled environment isdivided into increasingly fine subdivisions, typically boxes, which aredescribed as nodes in a tree (conventionally a binary tree), down to theleaves of the tree which represent the smallest level of subdivision inthe hierarchy. In general, depending on the particular scheme being usedand the particular geometry being modelled, the bounding volumerepresented by a given leaf node 206 could encompass exactly onegeometric primitive, more than one geometric primitive, or a part(fraction) of a geometric primitive. For example, a certain scheme mighthave a threshold on the number of primitives a leaf node 206 cancontain.

Note that whilst a primitive can be just a single point, primitives willmore often be a 2D or 3D shape and so primitives don't necessarilyalways fit perfectly into one bounding box or another. Bounding volumesmay overlap with one another and a given primitive (or part of it) cansometimes be found within bounding volumes other than those that lead toit in the BVH. Further, one can build a BVH that has multiple volumesparenting a single primitive.

FIGS. 3 a and 3 b give some examples. In FIG. 3 a , the bounding boxesof two leaf nodes 206 overlap, but each only points to their singlecorresponding primitive 301 (in this case a triangle) in the hierarchy.In FIG. 3 b , for the triangular primitive 301, instead of having onelarge bounding box, it might be beneficial to have two smaller boxes tobound it tighter. So there will be two leaf nodes 206 in the hierarchythat parent the same primitive 301.

Each of the nodes 202, 204, 206 may be represented by data held in arespective slot of a node buffer 110, with one slot per node. The nodebuffer 110 is maintained by the BVH module 102, and depending onimplementation may be implemented in general purpose memory, indedicated memory or a dedicated region of memory, or in dedicatedregisters.

For the sake of discussion, individual bounding volumes may be referredto herein as boxes, but this is not limiting and more generally boundingvolumes may take any shape. Any reference herein to a bounding box maybe replaced more generally with the term bounding volume or region ofspace.

To determine which primitives a given ray intersects with, the raytraversal unit 106 first determines which bounding box(es) at the toplevel of the tree the ray intersects with, then within that/thosebox(es) determines which box(es) at the next level down the rayintersects with, and so forth. This is more efficient than justcomparing every primitive against the ray. Note that the proceduredoesn't necessarily just stop at the first box the ray intersects with(i.e. it is not assumed that one box is occluded by the other), as a raycould hit a box but still miss all the primitives within that box.

In order to determine the bounding boxes represented by the differentnodes 204, 206 in the BVH 200, a simple way to divide the space wouldjust be to divide in half, then divide each half in half again, etc.;for example either by dividing the space in half by distance along acertain axis such as the x-axis, or by dividing either side of themedian coordinate along a certain axis such as the x-axis using the xcoordinates of the primitives' centroids (in other words, divide thespace such that half the primitive centroids fall one side of thedivision, and the other half of the primitive centroids fall the otherside). The centroid of a given primitive could be the centre of mass ofthe primitive itself (e.g. of the triangle), or the centroid of abounding box around the primitive.

To deal with the fact that a primitive may not be a single point and soprimitives don't always fit perfectly into one bounding box or another,a basic implementation of the simple method may use the centroid of aprimitive's bounding box to determine which side of a split it shouldgo—the primitives might not be entirely one side or the other and thisis where overlaps between boxes may get introduced. FIG. 4 a shows anexample of this basic “object partition” method which preserves a wholeprimitive 301 per leaf 206. Here the spots represent the centroids. Asomewhat more complex implementation might decide to ‘cut through’ theprimitives and split the box in two, creating a situation like shown inFIG. 4 b.

SAH (surface area heuristic) is a metric for scoring the way in which amodelled space is split between nodes of a BVH. For a given tree orsubtree, it measures the expected number of computational operationsrequired to determine whether a random ray intersects with a primitivein the tree or subtree. To score a tree or subtree (e.g. the whole BVHor a part thereof), each node at every level in the tree or subtree inquestion is scored individually, and then the score for the tree orsubtree as a whole is the sum of the scores of the individual nodes.

SAH is based on the surface area of the bounding volume relative to thesurface area of the root bounding volume, multiplied by its child count.Other metrics also exist that can be used in the same way. For exampleone could just using the surface area of the two boxes, instead ofsurface area multiplied by child count. SAH may be referred tothroughout the following description by way of example, but this is notlimiting and more generally this could be replaced by any metric forestimating the expected computational cost of determining whether a rayintersects with a primitive for a given BVH.

Typically when querying a BVH it is desired to find which primitive theray intersects first—so the ‘nearest’ intersection to the ray origin.The metric doesn't assume a primitive will be hit—the result of thequery could be that the ray misses all primitives. But if it doesintersect, then (at least in embodiments) the cost measured by themetric is the expected computational cost of finding the nearestintersection. So the metric measures the expected number of operationsto determine a result—hit or miss—and if multiple primitives are hit,the ‘nearest’ is used. This is how SAH works, for example.

For avoidance of doubt, note that the ray traversal might notnecessarily find the nearest intersection first. E.g. if a leaf node isfound that is intersected by a ray, and it contains multiple primitives,the ray traversal algorithm may test all those primitives to determinewhich are hit, and then determine which was nearest; or it may finddifferent leaf nodes that are intersected, and again any intersectedprimitives would be identified from the different nodes and then thenearest determined.

The BVH formation module 102 may be arranged to find the BVH tree whichminimizes SAH (or equivalently optimizes any such metric), i.e.minimizes the average computational cost that will be incurred in raytraversal when searching the tree to determine whether a ray intersectswith a primitive. N.B. the terms “minimize” or “optimize” as used hereindo not necessarily limit to finding the absolute perfect solution, butrather more generally, can also mean iteratively tending towards animproved solution which might not be the absolute minimum or optimalsolution of all theoretically possible solutions, but which is at leasta cumulation of the iterative process of refining the SAH score (or suchlike).

Particularly, the procedure of iteratively modifying the BVH to try toreduce the score itself incurs processing resource, so there is atrade-off to be made between the processing resource put into BVHoptimization and the likely savings this will achieve in ray traversal.

The optimization works by starting with some initial BVH tree (e.g.determined in the simple way mentioned above), then considering possiblecandidate reinsertions—moving a node from one parent to another in thetree. Only if the move (in isolation*) reduces the overall SAH will itbe executed by actually updating tree. (*There could be a situationwhere when two particular reinsertions are executed, with one making theother actually increase the SAH, but this is complex to consider soreinsertions are evaluated individually on the assumption that onaverage over multiple reinsertions one will usually get a net decrease).

This process of testing candidate reinsertions and then executing thereinsertions may be performed more than once in an iterative manner.

A reinsertion is where an input node I (along with any of itsdescendants) is removed and reinserted alongside a target node T in thetree of the BVH. An example is shown in FIG. 6 . An “input node” hereinjust means a node in the current BVH which is being moved in areinsertion (or considered for being moved in a candidate reinsertion),i.e. the input of the reinsertion operation. When an input node I ismoved, the bounding volumes of any upstream nodes affected by the movemay be shrunk or grown accordingly. For instance in the example shown,the bounds of the parent of P′ will be grown to accommodate the boundsof I.

Also, note how the parent P in the left-hand figure disappears, as inthis example the tree is a binary tree and a parent can't have only onechild. When input node I is removed, parent P has just one child, theinput's sibling node S. To maintain the binary structure, the parent Pis removed and replaced by the sibling S in the tree. “The slot of thisfreed node in the node buffer 110 can then be recycled to store a newparent P′ of both the input node I and target node T (the new sibling).This process both assumes and maintains a binary tree.

In the scenario where a lone sibling of the input node is left behind,and the old parent of the input node is removed, this may be referred toas “singleton removal”. Technically it is the old parent that is removedrather than the singleton itself (if the lone remaining old sibling iscalled the “singleton”), but this is equivalent to removing thesingleton and updating the properties of the parent to match. Inpractice the “singleton removal” is done by removing the parent ratherthan the singleton because the singleton already contains most of thedata needed (bounds, child pointer, child count etc), so this avoidsneeding to copy this information into the parent by simply removing theparent instead. So the term “singleton removal” can just mean editingthe tree to remove single child cases.

Reinsertions are identified by searching the hierarchy for a target nodethat maximises the expected SAH reduction for a given input node:

-   -   removing the node may shrink the bounds of nodes above it in the        tree, reducing SAH (it is also possible for a node to not affect        the bounds of nodes above it in the tree—e.g. if the node does        not touch any of the sides of the old parent's box, removing it        will make no difference to the parent);    -   reinserting the node may increase the bounds of nodes above,        increasing SAH (again it is also possible there will be no        effect); and    -   if the reduction outweighs the increase, there is a net benefit        to the SAH

This expected SAH reduction defines the score for a reinsertion.Reinsertions with positive scores (i.e. which would reduce SAH) areattempted.

FIG. 5 illustrates a method of BVH optimization. At step 510, the BVHformation module 102 forms the initial BVH. Optionally it may alsodetermine an overall SAH score for the BVH as a whole (or overall scoreaccording to some other metric for measuring estimated computationalcost that would be incurred by searching the BVH in ray traversal).However determining the overall score for the whole BVH is not essentialas will become apparent shortly.

At step 515 the BVH formation module searches for candidate reinsertionsthat would be beneficial to the BVH, i.e. that would reduce its SAH (orother such score, i.e. reduce the estimated computational cost thatwould be incurred by searching the BVH in ray traversal).

The search step 515 may be described as comprising three sub-steps. Insub-step 520, the BVH formation module 102 searches the tree of thecurrent BVH for possible candidate reinsertions. The search forcandidate reinsertions may consider all possible input nodes in thetree, or only a subset. In the latter case the subset may be determinedby a sparsity parameter. For each considered input node, the search mayselect one or more possible reinsertions of that node as initial orpreliminary candidates for scoring. In sub-step 530, the BVH formationmodule 102 scores each of the initial candidate reinsertions accordingto the change in SAH that it would produce if executed. If a reinsertionwould improve the score (would decrease the SAH, i.e. reduce theexpected computational cost of ray traversal) then in step 540 it may beadded to a list of beneficial candidate reinsertions. Otherwise if itwould worsen the score, the potential reinsertion will be disregarded.Note that when scoring a potential or candidate reinsertion, it is notnecessary to compute the SAH for the whole BVH. Instead it is possibleto only compute the delta in the SAH (or similar metric) that would beassociated with the candidate reinsertion, i.e. the change or differencein the metric that the candidate reinsertion would bring about ifexecuted. It is this delta which determines the potential benefit (ordetriment) of a candidate reinsertion. If the delta in SAH is negative(or more generally the metric represents a reduction in the estimatedcomputational cost associated with ray traversal) then the candidatereinsertion in question would be beneficial, but if the delta in SAH ispositive (or more generally the metric represents an increase in theestimated computational cost) then the candidate reinsertion in questionwould not be beneficial. A greater reduction in SAH represents a morebeneficial candidate reinsertion, and this may play a role indetermining which reinsertions to put forward as candidates forsubsequent steps. It may also determine which candidates are selectedfor execution, as will be discussed in more detail shortly (see step550).

Note also that FIG. 5 is somewhat schematized, and in practice sub-steps520-540 may be intermingled in time. I.e. it is not necessary to wait todetermine an entire set of possible candidate reinsertions first beforeperforming any of the scoring or adding candidates to the list ofbeneficial reinsertions, and instead the possible candidate reinsertionscould be scored as the search progresses.

Particularly, the searching 520 and scoring 530 may be performedsynergistically with one another. For example, in embodiments the search515 narrows down to only one candidate reinsertion per input node to beput forward for conflict checking at step 550. To do this, then for eachpotential input node considered in the search, the search may begin bytaking a first possible reinsertion of the input node and scoring thisfirst possible reinsertion (i.e. determining its delta), then scoring asecond possible reinsertion of the same input node and if thatreinsertion produces a greater reduction in SAH, the second possiblereinsertion replaces the first as the current candidate for the inputnode in question, but otherwise the first reinsertion remains. Then thesearch scores a third reinsertion and if this beats the currentcandidate, it replaces the first or second as the current candidate, andso forth. The search may progress over all possible reinsertions; oronly a subset of possible reinsertions according to some defined searchcriterion, and/or until some defined criterion for reduction in SAH ismet.

In such an implementation of the search and scoring, step 540 simplydetermines whether or not the best candidate for each input node doesindeed give a reduction in SAH (i.e. does reduce computational cost),and if so includes it on the list of beneficial candidates. This couldbe done after the search for the best reinsertion of each individualinput node, rather than waiting until the end of the search forreinsertions of all considered input nodes.

In embodiments, the search for possible reinsertions of a given inputnode may begin at the level of the input node (the node potentiallybeing moved), then move up the tree. So the first level of searches willbe on nodes below the input node's parent. Then the search moves up alevel to include nodes below the input node's grandparent, and so on.Note that nodes below the input node are not included in the search, asit is meaningless to consider moving a node below itself. The search maytry out all or a subset of the possible moves at each level. It may onlytrack the change in SAH up to the highest node affected by the move. Ifthe move reduces the SAH, it is added to the candidate list. Each nodewill have a single ‘best move’ found so far in the search. If a movereduces the SAH more than the current best, it replaces it as the bestmove. The search then moves up to the next level in the hierarchy, andrepeats this process, and so forth exhaustively all the way up thehierarchy.

At step 550, the BVH formation module 102 determines which of thereinsertions on the list of beneficial candidates to actually execute,i.e. to update the current BVH with to actually include in the tree (sofar the candidate reinsertions have only been considered by the BVHformation module 102 as possible or hypothetical moves for scoringpurposes). This could simply comprise selecting all the reinsertionsfrom the list, or taking a random selection, or executing the top M mostbeneficial, or such like. However, in embodiments step 550 may comprisea conflict management step.

Conflicts occur if two (or more) different candidate reinsertions on thelist would try to modify the same part of the tree, i.e. if there is anynode that both reinsertions would need to modify. An example would betwo candidate reinsertions having the same target node. Therefore inembodiments a conflict management process may be included to ensure thatif there are candidate reinsertions on the list that would conflict withone another on the candidate list, then only one of them gets executed.

Conflict management may be particularly relevant if reinsertions are tobe executed in parallel. When it comes to executing reinsertions, it isdesirable to be able to execute multiple of these reinsertions inparallel (simultaneously or concurrently), each by a different parallelprocess such as a thread or shader invocation. For instance differentones of the processes could be run on different parallel executionsunits, or different time slots in a barrel-threaded execution unit.

Potential reinsertions could conflict with each other—that is, try tomodify the same nodes during execution. When executed in parallel thisintroduces race conditions. For example, two reinsertions may share thesame target, and both would attempt to modify the target nodeaccordingly.

A locking strategy may be used to prevent conflicts in execution.According to this, each candidate reinsertion is represented by arespective reinsertion process or other such portion of code which can“bid” on behalf of the respective reinsertion (e.g. this may be done bya particular bidding thread of a reinsertion process formed from aplurality of threads). Once a candidate reinsertion has been defined,the BVH formation module 102 knows the effect it will have (the nodes itaffects and the change in SAH score it achieves). Each candidatereinsertion on the list of beneficial reinsertions must “bid” forownership of the node(s) that are affected by that reinsertion (orrather, the reinsertion process representing the reinsertion bids onbehalf of the candidate reinsertion). If two different candidatereinsertions would conflict (i.e., bid on one or more of the sameaffected nodes), the one with the best SAH improvement wins and theother had to be discarded. If two have the same SAH improvement, thereis a tie breaker criterion, e.g. the one with the largest input nodeindex wins.

A reinsertion must win all bids on the nodes it affects in order to beconsidered safe to execute in parallel. E.g. bids may be 64-bit unsignedintegers: the 32 most significant bits taken from the float score, andthe 32 least significant bits the index of the input node. The currentbid for a node may be updated by performing an atomic max with theproposed bid. This means reinsertions with higher scores win ownershipof affected nodes. The input index is used to deterministically settletiebreaks on reinsertions with the same score. After the biddingprocess, bids are checked and reinsertions that fail to win all therequired nodes are discarded from the current iteration.

In the context of bidding in conflict management, different strategiescan be employed to define how a node may be said to be “affected” by areinsertion. For example, one strategy may consider whether thereinsertion would change the topology of the tree connecting to or fromthe node in question (so change its parent, siblings or children).Another strategy may alternatively or additionally consider whether thereinsertion would change the bounds of the node in question.

In embodiments a policy of “sparsity” may be employed for reducing thenumber of conflicting reinsertions. This means at the search stage 515considering only a subset of nodes as input nodes for a given iteration.The idea is that the subsequently reduced set of selected reinsertionsare less likely to have conflicts with one another. Sparsity may beimplemented for example by considering only every nth node (e.g. everythird node) in the order stored in memory, or by an order indexed in thetree (location in memory doesn't necessarily map to position in tree,though preferably the nodes that are included as possible inputs shouldbe scattered around the tree, and nodes are typically stored breadthfirst or depth first). Another example would be to use a randomselection of nodes.

In embodiments, the subset may be determined by a sparsity parameter.The sparsity parameter may be settable.

In one particular example implementation, sparsity may be implementedusing a sparsity parameter, μ. Instead of processing every node in thehierarchy, every μth node is processed. A cycling offset is used toprocess a different set of nodes each iteration. For example, if μ=3,(and noting that the root node is usually stored at index 0, and isnever processed as an input) each iteration will process nodes atindices:

-   -   1, 4, 7, 10, . . .    -   2, 5, 8, 11, . . .    -   3, 6, 9, 12, . . .    -   1, 4, 7, 10, . . .

Fewer reinsertions means lower chance of conflicts, but also fewerpotential improvements to the hierarchy. In embodiments the sparsityparameter p may be decreased during the optimisation (see below), andthe optimal starting value may be scene dependent.

In some embodiments the sparsity parameter may be variable from oneiteration to the next. For example in embodiments the sparsity parametermay be set based on the current overall SAH of the BVH in the currentiteration, e.g. based on how well the total BVH is converging from oneiteration to the next.

At step 560 the method executes whichever candidate reinsertions wereselected from the list at step 550 (e.g. passed the conflict check).This means updating the structure of the current BVH in temporarystorage (e.g. in the node buffer 110) with the selected reinsertions,i.e. to actually include the selected reinsertions in the tree (asopposed to merely considering them as candidates as in preceding step).This may comprise recomputing the overall SAH of the current tree.

Steps 520-560 form one iteration. To recap, in embodiments eachiteration may comprise the following operations: for each input node,search for best target node to define a reinsertion;

-   -   for each reinsertion, bid on affected nodes;    -   for each reinsertion, check bids on affected nodes have been        won;    -   for each (successful) reinsertion, execute the reinsertion by        updating the BVH topology;    -   refit bounding volumes for all nodes in the hierarchy; and    -   calculate the new SAH of the hierarchy

At step 570, the BVH formation module 102 determines whether theiteration just performed at steps 520-560 is to be the last iteration.If not, the method loops back to step 520 and repeats from there usingthe newly updated BVH now as the current BVH. In embodiments which makeuse of the total overall SAH of the BVH (not essential), then the totalSAH of the graph may be recomputed at this point (or as a variant thiscould be done at only some iterations).

If on the other hand it is determined at step 570 that the lastiteration has been reached, then the method proceeds to step 580 wherethe BVH formation module 102 writes the final BVH to the BVH storage 104and triggers the ray traversal unit 106 to go ahead with performing raytraversal based on the latest version of the BVH now stored in the BVHstorage 104.

In embodiments the determination as to whether the final iteration hasbeen reached at step 570 could simply comprise determining whether apredetermined number of iterations have been performed, or a thresholdtime has elapsed, or such like. I.e. after a predetermined number ofiterations have been performed, or a predetermined time has elapsed,then at step 570 the method will determine that that was the lastiteration and proceed to step 580.

As another example however, the determination is made based on whether aconvergence threshold has been reached. There are diminishing returnswith each iteration, so once the SAH reductions converge to below somepredetermined threshold level, at step 570 the method may stop theiterations and proceed to step 580 so as to go ahead with ray traversal.In other words the above steps are repeated until the optimisationconverges to some predetermined degree. The convergence could bedetermined by comparing the total SAH of the BVH resulting from thelatest round of executions in the current iteration with the total SAHthat resulted from the previous iteration. Alternatively it could bedetermined by summing all the individual SAH scores (the individualdeltas) of all the executed reinsertions from the current iteration.

In embodiments, a second parameter, the score threshold, may be used todetermine whether the optimisation is progressing well. In this case ifthe reduction in SAH between iterations is below this threshold (or evennegative—that is, the SAH has increased) the sparsity parameter p willbe decremented. This repeats until μ=0, at which point the optimisationterminates.

Example Software Implementation

Computational work to be performed by a parallel processor can bearranged into so called “workgroups” and “threads”. A workgroup maycomprise one or more threads, where in general that plurality of threadscan be processed in series or in parallel (e.g. at a single core of agraphics processing unit). Workgroups may be processed independently ofeach other (e.g. at different graphics processing unit cores, or inseries at a single core of a graphics processing unit). Threads withinthe same workgroup may be able to synchronise with each other duringprocessing, and may also be able to share access during their processingto memory dedicated to the GPU core processing those threads (e.g.on-chip memory dedicated to the GPU core processing those threads). Bycontrast, different workgroups may not be able to synchronise with eachother during processing, and may not be able to share access duringtheir processing to memory dedicated to a certain GPU core. In the casewhere a workgroup is formed of a plurality of threads then this may bearranged as an array of threads (e.g. a one-dimensional, two-dimensionalor three-dimensional array of threads). The number of threads comprisedby a workgroup may be limited. The limit on the number of threadscomprised by a workgroup may be a hardware restriction (e.g. a limit onhow many threads can be processed in parallel on the availableprocessing hardware). In a common example, a workgroup may comprise upto 1024 threads. In this example, if more than 1024 threads are to beprocessed in accordance with the same computational program (e.g. shaderprogram), then more than one workgroup will be associated with thatcomputational program. For example, if 3000 threads are to be processedin accordance with the same computational program, then three workgroupsmay be associated with that computational program (e.g. two of whichcould be fully packed, the third being partially packed). It is to beunderstood that the “workgroup” and “thread” terminology used herein isnot intended to be limiting, and that other terminology could be used todescribe the same concepts. For example, a “thread” as described hereincould alternatively be referred to as a “shader invocation”, an“invocation” or a “work-item”, whilst a “workgroup” as described hereincould alternatively be referred to as a “thread block” or a“threadgroup”.

In the present context, a reinsertion process (or more briefly a“process”) performed in respect of a given input node may comprise asequence of stages, with a separate thread for each stage. The threadswithin the reinsertion process are linked by use of the same ID. Assuch, the reinsertion process could be described as a “thread set” or“program stream” comprising one or more threads. Threads fromreinsertion processes for different nodes, but relating to the same step(i.e. in respect of different nodes), may be grouped together intoworkgroups. The relationship between threads and reinsertion processesis explained in more detail below.

In embodiments each input node I that may potentially be the subject ofa reinsertion is processed through several ‘steps’ of a givenreinsertion process, by a respective thread for each step. The threadfor a given step may also be referred to as a shader invocation. Thesoftware implementing the BVH formation module 102 may comprise blocksof shader code—each defining a ‘step’ of the reinsertion process—thatcan be run multiple times to perform its step on different input nodes.For a given step, each thread (shader invocation) will have a unique IDthat can be used to identify the portions of data to process for theinput node in question (for example, using the ID to derive the inputnode index for that thread). Some or all of the threads of differentinput nodes for a given step may be executed in parallel with oneanother, as a workgroup. In embodiments, the programmer runs Nreinsertion processes by specifying the sequence of steps to run, andhow many workgroups are needed for each step, where the total number ofthreads for a step (in accordance with threads per workgroup, andworkgroups per step) is at least N. Note that different steps could havedifferent workgroup sizes. Lower-level scheduler hardware or softwaredetermines the exact ordering and parallel execution of threads, inaccordance with the composition of workgroups and steps defined by theprogrammer.

In embodiments, shader code is provided for the following steps:

-   -   Search: Finding the best reinsertion for a given input node    -   Bid: Making bids for a given reinsertion    -   Check: Checking bids have been won for a given reinsertion    -   Execute: Executing a reinsertion—changes to hierarchy topology    -   Refit: Updating bounding volumes (e.g. boxes) to reflect changes        to the hierarchy    -   SAH: Scoring the SAH of the hierarchy

The reinsertion process for a given input node comprises an invocationof each of the search, bid, check and execute shaders, sharing a commonID. I.e. the search, bid, check and execute shaders are invoked perinput node, and the different shaders invoked in respect of the samenode are linked by the use of the common ID. The refit and SAH shaders,which are invoked after the reinsertion processes have been performedfor an iteration, are not invoked per input node. Instead, the refitshader starts with one thread per leaf node. All threads of the refitshader will attempt to update the box of their parent—with only the lastthread being allowed to update a given node (i.e. if two threads try andupdate a node, only the second thread will actually do it). Thesesuccessful threads will then attempt to update the subsequent parent thenext level up, and so on until a final thread reaches the root. For theSAH shader, a thread is run for all nodes, not just input nodes that mayhave had a sparse selection.

So rather than having persistent threads processing each input nodethrough an entire iteration, there will be multiple threads running thesearch shader for their respective input nodes. Then further threadsrunning the bid shader for the selected reinsertions of those respectiveinput nodes, and so on.

For an example, say there are 3000 input nodes up for consideration, andit is desired to process (without any sparse selection of nodes for now)reinsertions for them. If there are 1024 threads to a workgroup, thesearch, bid, check and execute shaders will all have 3 workgroups run,with one thread per input node. (As there are more threads than inputnodes in this case, some threads may not have any work to do.)

If sparsity is employed, this just changes the mapping from thread IDsto indexes of input nodes in the node buffer. So if sparsity is 3,thread with ID 0 will process node 1 in the buffer, thread 1 willprocess node 4, thread 2 to node 7 etc. In the next iteration, thesemappings are offset by one to get a different set of nodes processed:thread 0 processes node 2, thread 1 processes node 5 etc.

In embodiments, all workgroups for one shader must finish before anystart for the next shader.

-   -   i. The search shader will conduct the full search to find a        reinsertion for node X in the buffer (where X is derived from        the thread ID). It will store the reinsertion by writing into        two other buffer slots: targets[X] and scores[X]. Other shaders        can then read these buffers later.    -   ii. The bid shader will make updates to a bids buffer. E.g. if        the reinsertion for node X affects node Y, the respective thread        for the reinsertion of node X will try and update bids[Y] to the        maximum of its current value and the proposed bid. Because        multiple threads could be trying to update bids[Y], this        operation is done atomically (i.e. sequentially for updates to        the same slot).    -   iii. Similarly, the check shader then goes through all the        affected nodes to ensure the bids have been won for the        reinsertion of node X. If not (i.e. it loses a conflict), it        sets scores[X]=0.    -   iv. The execute shader then makes the actual reinsertion of node        X, if scores[X]>0.

There is one thread for each step of the iteration, for each input node.And there may be various buffers corresponding to each node to workwith. Input nodes are handled on a per-thread-ID basis. Each step foreach respective input node is done by an individual thread. Each searchshader thread scores all the candidate reinsertions for its own inputnode, and the corresponding bid thread with the same thread ID willconduct bids on behalf of any beneficial candidate reinsertions of itsrespective node, and the corresponding execute thread with the samethread ID will update the BVH for any winning bids of the respectiveinput node.

There does not have to be any centralized coordinator to allocate inputnodes to threads. Instead each thread can derive the input node it isprocessing itself, from its own ID. In embodiments, any centralised workis just checking the progress of the optimisation, and is handledCPU-side. Example CPU pseudo-code may look something like this.

  while (!finished) {  RunSearchShaderOnGPU(num_workgroups)  while(retries < 3) {   RunBidsShaderOnGPU(num_workgroups)  RunCheckShaderOnGPU(num_workgroups)  RunExecuteShaderOnGPU(num_workgroups)   retries++  } RunRefitShaderOnGPU(num_workgroups)  RunSAHShaderOnGPU(num_workgroups)} Note: the loop “while (retries < 3) { . . . }” refers to an example ofthe conflict retries feature described later.

It will be appreciated that the above is just an example of how thetechniques disclosed herein may be implemented in software. Moregenerally, the software may be described as comprising a plurality ofprocesses (or streams of code) where each process performs at least partof the processing of a given respective input node, and at least partsof at least some of the processes or streams may be run in parallel withone another. More generally still, the software may be implemented inany serialized or parallel form or combinations thereof. Note also that“parallel” execution as referred to herein may be taken to cover eitherexecution through different duplicated parallel hardware resources, orconcurrent execution in different time slots of barrel threadedexecution unit, or a combination of these techniques.

Non-Binary Reinsertions

A constraint of the above optimisation algorithm in the form assumed byMeister is that a binary tree is input, and is maintained throughout theoptimisation. It is recognized herein however that it would be desirableto consider possible candidate reinsertions, at least in the search(step 515) for the purpose of being scored, that do not assume that abinary tree must always be maintained, as this will provide moreopportunities for reducing SAH. Depending on the scores, suchreinsertions may then be included among the list of beneficialreinsertions considered for selection (step 550) or indeed those whichare actually executed (step 560), e.g. if they clear the conflictmanagement (step 550). However, extending the concept of reinsertion toBVHs that are not binary trees can result in changes in the number ofnodes in the overall BVH, which is not something the Meister algorithmaccounts for, nor something the Meister paper considers and thus notsomething it suggests how to deal with.

Such reinsertions, that do not assume that a binary tree must always bemaintained, may include reinsertions whereby the tree of the BVHcomprises non-binary parts before and/or after the reinsertion. Thereinsertion may leave the old parent with more than one child, and/orgive the new parent more than two children. In embodiments, a candidatereinsertion or executed reinsertion may change a binary part of the treeinto a non-binary part. And/or, a candidate or executed reinsertions maychange a non-binary part of the tree into a binary part.

In embodiments binary reinsertions may be included as well, whether ascandidates for scoring or conflict checking, or as actual reinsertionsto be executed. As such, some of the reinsertions may still involvesingleton removal and insertion of a corresponding new parent for thetarget node and input node as described earlier in relation to FIG. 6 .

Thus embodiments disclosed herein now allow two-types of reinsertion,binary and non-binary. Allowing non-binary reinsertions opens up moreopportunities for reducing SAH. The search (515) may now systematicallyconsider both binary and non-binary possibilities.

Non-binary BVHs can have benefits for tracing performance and bandwidthreductions, so it is useful to allow non-binary inputs, that have beenbuilt with these benefits in mind, and produce non-binary hierarchiesthat have these benefits. Therefore, the two types of reinsertion may beused. The binary reinsertion may be implemented as discussed before,with a new node created to parent the target and input node. Thenon-binary reinsertion sees the input node added to the target's childlist. Both reinsertion types can be evaluated during the target searchfor each candidate target.

FIG. 7 illustrates the possibility of a non-binary reinsertion. Theleft-hand diagram within FIG. 7 shows an example of a current tree (orpart thereof) that may be encountered when allowing non-binary trees.This could either be the starting BVH or the BVH at a current round ofiteration. Either way, in the illustrated example the input node I isone of a group of three siblings, i.e. three children of the currentrespective parent (which would become the old parent under thereinsertions being considered). The middle diagram shows the possibilityof a binary reinsertion as discussed earlier with respect to FIG. 6 .The only difference here is that as the input node I leaves behind twoold siblings, there is no “singleton” left behind as discussedpreviously with respect to FIG. 6 , and so the old parent remains inplace (albeit with shrunken bounds). Thus, in this case, the reinsertionis binary, even though the sub-tree of the input node and its parent andsibling nodes is not binary in nature. The right-hand diagram shows analternative, non-binary reinsertion. Here, instead of becoming a siblingof the target node T and creating a new parent (+) of the target nodeand reinserted input node, as in the binary reinsertion case, insteadthe input node I is reinserted as a child of the target node T, forminga group of now three (or more) siblings in the destination branch of thetree, i.e. more than two children of the pre-existing parent or thetarget node T.

However, relaxing the binary requirement may introduce an additionalissue, as briefly mentioned above. It was previously guaranteed that theinput's parent node is freed (due to a singleton removal), and that thisnode is “recycled” as a parent for the input node I and target node T.In other words, the slot in the node buffer 110 that was previously usedfor the old parent can be re-used for the new parent. However, ifallowing a non-binary tree, this singleton removal may not occur.Instead a new node may need to be created without removing an old one.If no slot is freed up in the node buffer by removing an old node, thena new slot in the node buffer 110 will be needed to represent the newnode (+).

For example see FIG. 7 , right-hand diagram, where the input node I wasin a non-binary child list and so the old parent remains. Likewise, anew node is not always needed in the case of a non-binary reinsertion.If, as shown, an input node I is moved from a group of three children ofa given parent, leaving still a group of two children of the old parent,then the old parent has to be kept in the BVH. But the move mightinvolve creating a new parent for the moved node. E.g. in theillustrated example, instead of just adding I as another child of T atthe same existing level, as in the right-hand diagram of FIG. 7 , a newparent node (+) may be created for I and T, effectively performing abinary reinsertion as shown in the central diagram of FIG. 7 . Thereason is that it might be better in terms of SAH to create this newnode. Child count is a factor of SAH, so having a node with lots ofchildren can be costly. It's a question of weighing up whether at thispoint in the traversal it would be better to: a) definitely do 3 boxtests (T's children and I), or b) definitely do 2 box tests (T and I)and then maybe do 2 more box tests on T's children, if T is hit. So ifthe chances of hitting T and doing those extra 2 tests is less than 50%,the new node is worth it, as the expected tests will be, say,2+(2*25%)=2.5<3.

However, the number of slots in the node buffer 110 is a finiteresource, and so new nodes cannot necessarily be added at will. Toaccommodate the possible creation of a new node, it would be desirableto provide a scheme to track and allocate free node slots created andused by reinsertions. In embodiments, the optimisation operates on acertain operating range of node slots in the node buffer 110, preferablya fixed range (e.g. indexes 0 to n). Including new nodes in thisoperating range assists in reducing SAH, as the new nodes could be usedas input nodes or selected as target nodes in future iterations.

According to embodiments disclosed herein, free node slots in theoperating range will be allocated to the reinsertions that need them.This is preferably done deterministically; because otherwise, allowing anew node to be created at different indexes could, for example, changewhether or not it is processed in the next iteration (thus allowing fordifferent possible resultant BVHs after multiple iterations, which wouldresult in different possible performances when rendering the same scene,which is not desirable).

As discussed earlier, in embodiments each input node is processed by arespective reinsertion process comprising one or more threads, where atleast parts of at least some of the different processes may be run inparallel with one another. For instance, each process may be configuredto perform the search, bid, check and execute-reinsertion steps onbehalf of its respective input node. E.g. each process may comprise aplurality of threads, where each of the plurality of threads comprisesan invocation of a different respective one of a set of shaders: asearch shader, a bid shader, a check shader and an execute-reinsertionshader. Each reinsertion process is identified by a respective processID. In embodiments where a process comprises a plurality of threads thenthe threads of that process may each be identified as part of the sameprocess by the respective process ID.

The scheme implemented sees a list maintained for each process ID, eachlist indicating free node slots created through singleton removals bythreads associated with that ID. Threads associated with a particular IDare able to recycle the nodes indicated in the list for that ID forfuture binary reinsertions. These per-ID free lists ensure deterministicallocation of nodes to reinsertions. Binary reinsertions will only beevaluated if a freed node slot is available to the reinsertion process(e.g. if a previous reinsertion process using the same ID created afreed node slot), or will be immediately created by that processperforming a singleton removal. While free node slots will not always beavailable with this scheme (i.e. a free node slot associated with one IDwill not be available to a reinsertion process associated with adifferent ID), it is still sufficient to produce good SAH reductions (byallowing nodes to be created within the operating range) in adeterministic manner.

To elaborate, it would in principle be possible to just create anddestroy slots in memory as-and-when needed, but it would be preferablenot to operate like this. This would require a global allocation scheme,where parallel reinsertion processes can be given any free slot in thememory if/when they need one. This has two issues. The first is speed.Multiple parallel processes will want to be allocated a free slotsimultaneously, but the global record of where these slots are must beupdated atomically (i.e. one at a time). So each process must wait itsturn to get the next free slot, wasting time in doing so. The secondreason is determinism. The order in which reinsertion processes areallocated nodes would not be fixed. So nodes could be written atdifferent places in the buffer 110 on different runs of theoptimisation. Due to the sparsity feature described earlier, this couldresult in different nodes being selected in the next iteration, anddifferent changes will be made to the hierarchy.

Instead therefore, in embodiments, at the beginning (step 510) themethod starts with a certain number of slots (e.g. 64000) allocated inmemory (the node buffer 110), each mapped to an existing node (oralternatively this could be relaxed to include some unmapped slots toplay with). Then, as the method proceeds through one or more iterationsof the search and execution cycle (steps 520-560), it might happen thata node gets destroyed without a new one needing to be created. E.g. theinput node was a sibling of only one other node, so its old siblingbecomes a singleton and so the old parent gets removed; but the nodebeing moved gets added as a sibling of an existing group of siblings. Ifa node's slot gets freed in this way in one iteration, it then makes anode slot available to be used for a new node by the process with thesame ID in the same iteration or a subsequent iteration. Thus a new nodeslot is made available to expand the set of possible moves that can beconsidered. So during the search of the next iteration it may now bepossible to consider a candidate move/reinsertion that would create anew node, whereas those possibilities were ruled out in the previousiteration. Or the freed node's slot could be recycled in the currentiteration. E.g. if the input node is one of two children, it can beknown that removing it will cause this singleton removal. That way itcan be known that a free node will become available and therefore thatbinary reinsertions can also be considered during the search. Becausefree nodes are tracked per-process ID, then in such embodiments a freednode slot can only be used by a reinsertion process with the same ID asthe process that removed the corresponding node, not from any otherreinsertions.

In embodiments, within a given iteration there is one reinsertionprocess per input node (each process comprising a thread or shaderinvocation, or a sequence of threads or shader invocations with the sameID, as discussed earlier), and many of these reinsertion processes (inrespect of different input nodes) may run in parallel. In subsequentiterations the process ID is reused for another reinsertion process,e.g. to process the same input node at its new location in the tree, orto process an entirely different node. Preferably, a given reinsertionprocess can only re-use a node slot that has been freed by itself oranother process associated with the same ID. Otherwise the method wouldnot be deterministic as it would depend on the thread scheduler. I.e. itwould be desirable (e.g. for testing purposes) to always get the sameresult for the same frame or point in time of the same scene. But ifslots could be re-used between parallel processes, the result woulddepend on whether thread 1 or 2 (for example) happened to get scheduledfirst from whatever point the process started, and thus get first “dibs”on the recycled slot.

Note: this issue does not depend on which SAH bid was higher, since thisis separate from the bidding on existing nodes to avoid conflicts.Parallel processes try to get free nodes once they've been confirmed as‘winning’ all their bids. Imagine there is a global list of all the freeslots, and each thread that needs one must remove the next slot in thelist to claim it. It can't be known whether thread 1 or thread 2 gets toread and update that list first, so it would not be deterministic whichthread gets which node.

Conflict Retries

In the algorithm of Meister, if a candidate reinsertion loses a bid inthe conflict management, it is simply discarded. But this is potentiallywasteful as that doesn't necessarily mean the candidate reinsertion wasuseless, just because it can't be executed in parallel with another,winning reinsertion within a given iteration.

Searches and scoring are expensive in terms of computational cost.Together they form by the far longest phase of an iteration, so anythingthat can reduce the number of searches required to reach a givenreduction in SAH, and/or which maximizes the improvement per search, isdesirable.

In Meister's method, after conflict resolution, any reinsertion thatfailed to win ownership of all the nodes it required is simply abandoned(not used). It is recognized herein that this is an inefficiency,especially given the cost of finding these reinsertions.

Even with the use of sparse inputs, conflicts are still common and limitthe SAH reduction for an iteration.

Conflicts prevent the concurrent execution of reinsertions, but may notinvalidate them. Therefore, according to embodiments disclosed herein,the method may include a scheme for conflict retries, to reattempt theseotherwise discarded reinsertions. In other words the method can re-try alosing reinsertion, after the execution of the winner but still withinthe same iteration. This increases the number of reinsertions—andtherefore the improvement to the hierarchy—executed from each searchphase.

For the sake of computational efficiency, in embodiments the re-triedreinsertion is not re-scored; though in other implementations one coulddo that, i.e. recompute the delta in SAH or other such metric associatedwith each candidate reinsertion that is to be potentially retried (asthe hierarchy has changed since it was last scored).

Either way, preferably it should be checked whether the reinsertion inquestion is still valid (the new hierarchy could make it nonsensical).Also the retried reinsertion should preferably still be conflict checkedagain against any other retried reinsertions trying to affect the samepart of the tree.

Conflicted reinsertions may be invalidated by changes to the hierarchywhen executing a previous set of reinsertions within the same iteration,for one or more of the following reasons:

-   -   an input node or target node having been freed during singleton        removal;    -   an input node or target node's slot in the node buffer having        been freed during singleton removal and subsequently recycled as        a new parent in a binary reinsertion, so the slot now represents        a completely different node;    -   a target node is now a descendent of the input (a node cannot be        moved within its own subtree); and/or    -   the reinsertion was binary and required a free node slot to be        created from a singleton removal, but there will no longer be        one created when removing the input.

Hence in embodiments, after the first set of reinsertions are executed,a set of one or more second reinsertions which previously lost theconflict check may be tried again, within the same iteration. Inembodiments this may comprise:

-   -   conflicted reinsertions are validated according to the above        criteria, discarding those that fail;    -   bids are reset to zero on all nodes;    -   remaining reinsertions can rebid on the nodes they affect;    -   bids are checked, to avoid conflicts; and    -   remaining reinsertions are executed.

As mentioned, the deltas in the individual SAHs need not be recomputedin between retries within a given iteration. Instead it may be assumedthat the delta value for each retried reinsertion remains approximatelythe same. For these retried reinsertions it will sometimes happen thatthis approximation is not the case in reality, and a particularindividual retried reinsertion could in fact actually increase the SAH;but nonetheless, since the scores of the reinsertions would not tend tochange much, the assumption made by not rescoring will still result inan overall reduction in SAH on average for multiple retries over aniteration. Alternatively however, in other embodiment, the deltas in theindividual SAH score (or other such metric) may be recomputed for thepotentially retried candidates in between rounds of retries within thesame iteration. In this case only those candidate reinsertions thatwould still give a reduction in SAH (or improvement of another suchmetric), or greater than a threshold reduction or improvement, areretried.

In embodiments, the total SAH score for the BVH is updated once periteration after all the updates, rather than after each set of updates.However it would also be possible to update the SAH score for the BVHafter the first set of updates (i.e. after the execution of thefirst-time winner reinsertions), and then update the score for the BVHagain after the second set of updates (the execution of the reinsertionsthat won the in round of retry). A further possibility is to not updatethe SAH score after every iteration, or even not to compute the totaloverall score of the BVH at all. In other words, e.g. for reasons ofcomputational efficiency, the current score for the BVH may not beconsidered when deciding whether to continue with a further iteration(at 570 in FIG. 5 ), or evaluate reducing a sparsity parameter aspreviously discussed. Therefore, in cases such as these, an updated SAHscore does not need to be computed. For example, in the extreme case, itmay be decided to perform a fixed number of iterations, and so a SAHscore for the BVH is never required to decide whether to iterate or endthe optimisation. Omitting the rescoring between each iteration may beadvantageous by reducing the calculations and thus processing to performthose iterations. All such approaches are valid.

A similar choice applies to refits of bounding boxes, which can beperformed after each set of updates, or after each iteration. Note thatupdating the SAH score for the BVH after a set of updates (whetherbetween iterations, or before retries within an iteration) wouldpreferably require bounding volumes to be up to date. If it is desiredto update the SAH score for the BVH, up-to-date bounds will be required.However if a different metric is being used for the score, that does nottake into account the bounding volumes (e.g. just scoring child count),then the bounding volumes don't need refitting. Also, note that withconflict retries, the topology may be updated over multiple sets ofreinsertions, including retries. Then after all retries in an iterationthe method may sweep the BVH to refit bounding volumes to reflect thosetopological changes. So if re-evaluating a reinsertion between retries,the bounding volumes would not necessarily be up to date. If it isdesired to update the SAH delta for a reinsertion between retries,up-to-date bounds will be required.

In embodiments, the retrying can be repeated several times, to extractmore updates to the hierarchy from a single search step. After allretries, the iteration is wrapped up with bounding box refits and SAHrescore steps as before. While the hierarchy may have changed between areinsertion being identified and executed, there is a net benefit tothese retries. As shown in FIG. 13 , optimisations with retries improveSAH faster and can converge to a lower final score.

In some embodiments, the reinsertions to be (potentially) retried may berescored before their bids are resubmitted. I.e. their SAH reduction isrecomputed after the previous winners in the current iteration have beenexecuted, and preferably after the bounding volumes have beenrecalculated in the updated BVH. In this case, only those reinsertionsthat are still beneficial after their score has been recomputed willprogress to the re-bidding stage. Alternatively however, in otherembodiments, to save on computation, the scores of the reinsertions tobe retried are not rescored, and instead any that is still valid mayprogress to the re-bidding stage.

Note also: while sparsity does not eliminate conflicts, only reduces thenumber of conflicts, embodiments may nonetheless employ sparsity as wellas conflict retries. Alternatively sparsity need not be employed, andinstead all the possible input nodes could be considered in the search.

FIG. 10 shows an example of a conflicted reinsertion that may beretried. Again it may be assumed that the candidate reinsertion of eachinput node is represented by a different reinsertion process (eachcomprising one or more threads.

Say that process 1 wants to perform a reinsertion to move its input nodeI from P to T as its new parent, and that process 2 wants to perform adifferent reinsertion to move another input node from elsewhere in thetree—say node J—to T as its new parent. Even in a non-binary examplewhere T can have a flexible number of children, this should preferablynot be done in parallel as otherwise the effect would be random—process2 could end up overwriting the result of process 1 or vice versa.

The conflict management resolves this—both process 1 and 2 can bid totake ownership of T (and other nearby nodes whose topology would beaffected by the proposed reinsertion). However conventionally the losingprocess (e.g. process 2 if process 1 won the bid) would have to abandonits candidate reinsertion, wasting the work that was done searching forthat reinsertion, even though the losing reinsertion may still be validand beneficial even after the winning reinsertion has been executed.

By allowing conflict retries on the other hand, both reinsertions canpotentially still be used. Say that process 1 wins the initial bid overT. Since (in the non-binary case), this does not exclude thread 2 alsomaking its addition, process 2's reinsertion can be re-tried.

All the reinsertion processes with winning reinsertions execute theirreinsertions, and there is a barrier to wait for all of them to finish,then threads with losing reinsertions can try again. This is slower thandoing all the reinsertions without a barrier, but that is not preferreddue to conflicts.

The above described scheme of conflict retries is not limited tonon-binary reinsertions or BVHs that allow non-binary trees. FIG. 11shows and example of a reinsertion that may be re-tried in a purelybinary scenario. This is like the example in FIG. 10 , except that bothI and J are moved in binary reinsertions. After the first reinsertion ofI, T is still a valid target, so J can be paired with it in a secondreinsertion.

Reduced Search Scope

In principle a node could be reinserted anywhere, but reinsertions aremore likely to give a good score if the destination of the input node isclose to its old position in the tree. It is recognized herein thereforethat it would be possible to reduce the computational cost of the searchfor candidate reinsertions, by restricting the scope of the search (step515), without being likely to have an undue effect—if any—on thereduction in SAH that is achieved.

Most improvement in SAH is made by changes closer to the root node ofthe BVH. Therefore in embodiments the method may be restricted to onlytrying to move nodes (i.e. only considering possible input nodes) abovea certain depth in the hierarchy, i.e. those closer to the root. Thiscould be thought of as a “search floor” in the tree. Or as a variant ofthis, the method could employ less sparsity closer to the root than atthe leaves. More generally, the restriction may be described by sayingthat: the input nodes are restricted to being from a subset close to theroot, and this could be achieved by, e.g., a restriction on depth, orusing a breadth-first ordering to get some number of nodes (with the2{circumflex over ( )}k approach below being an example of this).

Closer to the root, nodes are associated with larger bounding volumesand so with larger surface areas, and so the potential savings aregreater. This presents an opportunity to reduce the scope of theoptimisation (i.e. the scope for search), with limited impact on thefinal hierarchy quality.

In embodiments, there may be two constituent rules to this restriction.The first is that input nodes are selected from a subset of nodes closeto the root (e.g. not lower than a depth D, or first N nodes in breadthfirst order). This reduces work for the whole iteration as there arefewer reinsertions to process. The second rule is that target nodes mayalso be restricted to this subset. This reduces the search space. Thesecond rule requires the first because it wouldn't be advantageous torestrict targets to the top of the tree if an input node was far away atthe bottom of the tree.

In embodiments, the hierarchy is read in to the node buffer 110breadth-first, meaning that nodes are sorted according to their distancefrom the root. The first 2^(k) nodes in the buffer then define the scopeof the optimisation—input nodes and/or target nodes must be within this‘operating range’. Reducing this node count results in less nodes toprocess each iteration, and also a reduced search space when findingtarget nodes.

Refits and rescores of the hierarchy also benefit, as nodes outside ofthis range will have fixed bounds and SAH scores, so do not need to beaccounted for between iterations.

The selection of k introduces a time vs. quality trade off, wherereducing the scope saves optimisation time but may impact finalhierarchy quality. The method selects k relative to the number of nodesinput. For example, if a hierarchy has 2¹⁷ nodes (rounded up to a powerof two), the first 2¹⁴ nodes could be a processed—a difference of 3 onthe value of k. FIG. 14 a shows the impact of this difference, dk,against an unconstrained approach.

Significant run time savings can be made for very little cost inhierarchy quality. By way of example, dk=3 may be selected for a goodbalance between the two.

A fixed upper bound, in this case of k 16, is also set, to prevent verylong run times.

So for an input hierarchy with n nodes and dk=3:

k=min{┌log₂(n)┐−3.16}

This is just an example. More generally, any number M of nodes withinsome defined depth of the root may be used, instead of necessarily2{circumflex over ( )}k. Using M will still have the same effect andtime-quality trade-offs.

Furthermore, as an alternative or additional restriction on the searchscope, some unlikely possibilities may be culled without explicitlyscoring them, by instead setting a “search ceiling”, i.e. a maximumheight which can be affected in the tree above the candidate input node.E.g. if the ceiling is 3, then only moves to other positions below thegreat grandparent can be considered.

This narrows the search space when finding the target node. The majorityof optimal target nodes are found relatively close to the input,compared to the extents of an unconstrained search.

In embodiments, the search progresses as follows. The highest nodereached during the search is tracked, with the current search spacebeing the subtree rooted at this node (minus the input node and it'sdescendants). Once the current extents have been searched (depth-first),the highest node is moved up to its parent, expanding the search space.In the Meister paper this continues until the highest node is the root.See FIG. 12 .

By limiting the distance between the input node and highest node, theextents of the search can be easily restricted. FIG. 14 b illustratesthe trade-off between run time and hierarchy quality when selecting thisheight limit. By way of example, a maximum height around 4 or 5 getsgood run time savings at little cost.

The search ceiling could be used independently of the search floor or incombination.

Example System Implementation

FIG. 8 shows a computer system in which the graphics processing systemsdescribed herein may be implemented. The computer system comprises a CPU802, a GPU 804, a memory 806 and other devices 814, such as a display816, speakers 818 and a camera 819. A processing block 810(corresponding to the logic 100 of FIG. 1 ) is implemented on the GPU804. In other examples, the processing block 810 may be implemented onthe CPU 802. The components of the computer system can communicate witheach other via a communications bus 820. A store 812 (which maycorrespond, at least in part, to memory 104 in FIG. 1 ) is implementedas part of the memory 806.

The logic of FIGS. 1 to 7 is shown as comprising a number of functionalblocks. This is schematic only and is not intended to define a strictdivision between different logic elements of such entities. Eachfunctional block may be provided in any suitable manner. It is to beunderstood that intermediate values described herein as being formed bythe logic need not be physically generated by the logic at any point andmay merely represent logical values which conveniently describe theprocessing performed by the logic between its input and output.

The logic described herein may be embodied in hardware on an integratedcircuit. The logic described herein may be configured to perform any ofthe methods described herein. Generally, any of the functions, methods,techniques or components described above can be implemented in software,firmware, hardware (e.g., fixed logic circuitry), or any combinationthereof. The terms “module,” “functionality,” “component”, “element”,“unit”, “block” and “logic” may be used herein to generally representsoftware, firmware, hardware, or any combination thereof. In the case ofa software implementation, the module, functionality, component,element, unit, block or logic represents program code that performs thespecified tasks when executed on a processor. The algorithms and methodsdescribed herein could be performed by one or more processors executingcode that causes the processor(s) to perform the algorithms/methods.Examples of a computer-readable storage medium include a random-accessmemory (RAM), read-only memory (ROM), an optical disc, flash memory,hard disk memory, and other memory devices that may use magnetic,optical, and other techniques to store instructions or other data andthat can be accessed by a machine.

The terms computer program code and computer readable instructions asused herein refer to any kind of executable code for processors,including code expressed in a machine language, an interpreted languageor a scripting language. Executable code includes binary code, machinecode, bytecode, code defining an integrated circuit (such as a hardwaredescription language or netlist), and code expressed in a programminglanguage code such as C, Java or OpenCL. Executable code may be, forexample, any kind of software, firmware, script, module or librarywhich, when suitably executed, processed, interpreted, compiled,executed at a virtual machine or other software environment, cause aprocessor of the computer system at which the executable code issupported to perform the tasks specified by the code.

A processor, computer, or computer system may be any kind of device,machine or dedicated circuit, or collection or portion thereof, withprocessing capability such that it can execute instructions. A processormay be any kind of general purpose or dedicated processor, such as aCPU, GPU, System-on-chip, state machine, media processor, anapplication-specific integrated circuit (ASIC), a programmable logicarray, a field-programmable gate array (FPGA), or the like. A computeror computer system may comprise one or more processors.

It is also intended to encompass software which defines a configurationof hardware as described herein, such as HDL (hardware descriptionlanguage) software, as is used for designing integrated circuits, or forconfiguring programmable chips, to carry out desired functions. That is,there may be provided a computer readable storage medium having encodedthereon computer readable program code in the form of an integratedcircuit definition dataset that when processed (i.e. run) in anintegrated circuit manufacturing system configures the system tomanufacture logic configured to perform any of the methods describedherein, or to manufacture a logic comprising any apparatus describedherein. An integrated circuit definition dataset may be, for example, anintegrated circuit description.

Therefore, there may be provided a method of manufacturing, at anintegrated circuit manufacturing system, logic as described herein.Furthermore, there may be provided an integrated circuit definitiondataset that, when processed in an integrated circuit manufacturingsystem, causes the method of manufacturing the logic to be performed.

An integrated circuit definition dataset may be in the form of computercode, for example as a netlist, code for configuring a programmablechip, as a hardware description language defining hardware suitable formanufacture in an integrated circuit at any level, including as registertransfer level (RTL) code, as high-level circuit representations such asVerilog or VHDL, and as low-level circuit representations such as OASIS®and GDSII. Higher level representations which logically define hardwaresuitable for manufacture in an integrated circuit (such as RTL) may beprocessed at a computer system configured for generating a manufacturingdefinition of an integrated circuit in the context of a softwareenvironment comprising definitions of circuit elements and rules forcombining those elements in order to generate the manufacturingdefinition of an integrated circuit so defined by the representation. Asis typically the case with software executing at a computer system so asto define a machine, one or more intermediate user steps (e.g. providingcommands, variables etc.) may be required in order for a computer systemconfigured for generating a manufacturing definition of an integratedcircuit to execute code defining an integrated circuit so as to generatethe manufacturing definition of that integrated circuit.

An example of processing an integrated circuit definition dataset at anintegrated circuit manufacturing system so as to configure the system tomanufacture the logic will now be described with respect to FIG. 9 .

FIG. 9 shows an example of an integrated circuit (IC) manufacturingsystem 902 which is configured to manufacture logic as described in anyof the examples herein. In particular, the IC manufacturing system 902comprises a layout processing system 904 and an integrated circuitgeneration system 906. The IC manufacturing system 902 is configured toreceive an IC definition dataset (e.g. defining logic as described inany of the examples herein), process the IC definition dataset, andgenerate an IC according to the IC definition dataset (e.g. whichembodies logic as described in any of the examples herein). Theprocessing of the IC definition dataset configures the IC manufacturingsystem 902 to manufacture an integrated circuit embodying logic asdescribed in any of the examples herein.

The layout processing system 904 is configured to receive and processthe IC definition dataset to determine a circuit layout. Methods ofdetermining a circuit layout from an IC definition dataset are known inthe art, and for example may involve synthesising RTL code to determinea gate level representation of a circuit to be generated, e.g. in termsof logical components (e.g. NAND, NOR, AND, OR, MUX and FLIP-FLOPcomponents). A circuit layout can be determined from the gate levelrepresentation of the circuit by determining positional information forthe logical components. This may be done automatically or with userinvolvement in order to optimise the circuit layout. When the layoutprocessing system 904 has determined the circuit layout it may output acircuit layout definition to the IC generation system 1006. A circuitlayout definition may be, for example, a circuit layout description.

The IC generation system 906 generates an IC according to the circuitlayout definition, as is known in the art. For example, the ICgeneration system 906 may implement a semiconductor device fabricationprocess to generate the IC, which may involve a multiple-step sequenceof photo lithographic and chemical processing steps during whichelectronic circuits are gradually created on a wafer made ofsemiconducting material. The circuit layout definition may be in theform of a mask which can be used in a lithographic process forgenerating an IC according to the circuit definition. Alternatively, thecircuit layout definition provided to the IC generation system 906 maybe in the form of computer-readable code which the IC generation system906 can use to form a suitable mask for use in generating an IC.

The different processes performed by the IC manufacturing system 902 maybe implemented all in one location, e.g. by one party. Alternatively,the IC manufacturing system 902 may be a distributed system such thatsome of the processes may be performed at different locations, and maybe performed by different parties. For example, some of the stages of:(i) synthesising RTL code representing the IC definition dataset to forma gate level representation of a circuit to be generated, (ii)generating a circuit layout based on the gate level representation,(iii) forming a mask in accordance with the circuit layout, and (iv)fabricating an integrated circuit using the mask, may be performed indifferent locations and/or by different parties.

In other examples, processing of the integrated circuit definitiondataset at an integrated circuit manufacturing system may configure thesystem to manufacture logic without the IC definition dataset beingprocessed so as to determine a circuit layout. For instance, anintegrated circuit definition dataset may define the configuration of areconfigurable processor, such as an FPGA, and the processing of thatdataset may configure an IC manufacturing system to generate areconfigurable processor having that defined configuration (e.g. byloading configuration data to the FPGA).

In some embodiments, an integrated circuit manufacturing definitiondataset, when processed in an integrated circuit manufacturing system,may cause an integrated circuit manufacturing system to generate adevice as described herein. For example, the configuration of anintegrated circuit manufacturing system in the manner described abovewith respect to FIG. 9 by an integrated circuit manufacturing definitiondataset may cause a device as described herein to be manufactured.

In some examples, an integrated circuit definition dataset could includesoftware which runs on hardware defined at the dataset or in combinationwith hardware defined at the dataset. In the example shown in FIG. 9 ,the IC generation system may further be configured by an integratedcircuit definition dataset to, on manufacturing an integrated circuit,load firmware onto that integrated circuit in accordance with programcode defined at the integrated circuit definition dataset or otherwiseprovide program code with the integrated circuit for use with theintegrated circuit.

The implementation of concepts set forth in this application in devices,apparatus, modules, and/or systems (as well as in methods implementedherein) may give rise to performance improvements when compared withknown implementations. The performance improvements may include one ormore of increased computational performance, reduced latency, increasedthroughput, and/or reduced power consumption. During manufacture of suchdevices, apparatus, modules, and systems (e.g. in integrated circuits)performance improvements can be traded-off against the physicalimplementation, thereby improving the method of manufacture. Forexample, a performance improvement may be traded against layout area,thereby matching the performance of a known implementation but usingless silicon. This may be done, for example, by reusing functionalblocks in a serialised fashion or sharing functional blocks betweenelements of the devices, apparatus, modules and/or systems. Conversely,concepts set forth in this application that give rise to improvements inthe physical implementation of the devices, apparatus, modules, andsystems (such as reduced silicon area) may be traded for improvedperformance. This may be done, for example, by manufacturing multipleinstances of a module within a predefined area budget.

The applicant hereby discloses in isolation each individual featuredescribed herein and any combination of two or more such features, tothe extent that such features or combinations are capable of beingcarried out based on the present specification as a whole in the lightof the common general knowledge of a person skilled in the art,irrespective of whether such features or combinations of features solveany problems disclosed herein. In view of the foregoing description itwill be evident to a person skilled in the art that variousmodifications may be made within the scope of the invention.

According to one aspect disclosed herein, there is provided a method asset out in the Summary section.

In embodiments, in each of one, some or all of the candidatereinsertions, the candidate reinsertion would reinsert only its onerespective input node to another branch of the tree.

In embodiments, in each of one, some or all of the candidatereinsertions, the candidate reinsertion increases a number of childnodes at the new parent or adds a new node to the data structure as thenew parent.

In embodiments, the input nodes are limited to being at or above ahierarchical level in the tree of the current BVH a predetermined numberof hierarchical levels below the root node.

In embodiments, the new parent may be limited to being at or above ahierarchical level in the tree of the current BVH a predetermined numberof hierarchical levels below the root.

In embodiments, said plurality of input nodes may be only a subset of atotal number of nodes in the tree, the subset excluding the root node aswell as one or more internal nodes and/or leaf nodes.

In embodiments, at at least one layer in the tree of the current BVH,the subset may comprises only every Nth node within that layer, whereinN is an integer greater than two.

In embodiments, for each of said plurality of input nodes in any givenone of said iterations, said search for at least one candidatereinsertion may or may not successfully find at least one candidatereinsertion that would reduce the expected computational cost accordingto said metric.

In embodiments, for each of said plurality of input nodes, saidsearched-for at least one candidate reinsertion may consist of a singlebest candidate reinsertion, among multiple possible candidatereinsertions of the respective input node, that according to said metricwould give a greatest reduction in the expected computational cost.

In embodiments, said searching for the candidate reinsertions comprises,for each respective one of the plurality of input nodes: scoring each ofa respective plurality of possible reinsertions for the respective inputnode according to said metric, to determine a difference in theestimated computational cost that the reinsertion would achieve; andfrom among the multiple possible reinsertions for the respective inputnode, identifying the reinsertion which gives the lowest difference(i.e. biggest reduction), and if the lowest difference for therespective input node is a reduction in the expected computational cost,selecting the identified reinsertion as the at least one searched-forcandidate reinsertion for the respective input node.

In embodiments, the method may comprise determining a starting scorebeing a score of the starting BVH according to said metric, wherein thefirst iteration starts with the starting score as a current score of thecurrent BVH. Each iteration may comprise updating the current score toaccount for the first update.

In embodiments the method may comprise, after said one or moreiterations, searching the tree of the current BVH to determine whether amodelled ray intersects with any of the primitives.

In embodiments the method may comprise outputting graphical data forcontrolling a screen to render a scene representing at least part of themodelled environment, including a lighting effect based on the modelledray.

In embodiments, the one or more selected reinsertions selected forinclusion in the first update may be selected according to a conflictcheck to determine whether any group of the candidate reinsertions wouldaffect a same part of the tree of the current BVH as one another, and ifso selecting only one of the group to include in the first update.

In embodiments, said one of the group selected for the first update maybe selected based on being the candidate reinsertion from among thegroup that gives the greatest reduction in the expected computationalcost according to said metric.

In embodiments, at least one of the iterations may further comprise,after the first update, performing a second update within the sameiteration to update the current BVH with another of said group.

In embodiments, the second update may comprise retrying a plurality ofretried reinsertions remaining in the group after the first update, theretrying comprising: evaluating whether each of the retried reinsertionsmeet one or more criteria, and selecting one of the retried reinsertionsthat meets all of the one or more criteria as said other reinsertion toinclude in the second update.

In embodiments, the one or more criteria may comprise one, more or allof:

-   -   that the retried reinsertion is still valid following the first        update,    -   that the retried reinsertion is not still conflicted with        another, more beneficial one of the retried reinsertions, and/or    -   that the retried reinsertion would still lower the expected        computational cost according to said metric.

In embodiments, said other of the reinsertions selected for the secondupdate may be selected based on being the reinsertion, from among theretried reinsertions that meet the one or more criteria, that gives thegreatest reduction in the expected computational cost according to saidmetric.

In embodiments, each iteration may comprise updating the current scoreto account for the first and second updates.

In embodiments, the processor may comprise a buffer comprising aplurality of slots, each respective one of the nodes in the tree beingrepresented as an entry in a respective one of the plurality of slots.Said plurality of slots may be a fixed number of slots.

Different input nodes may be processed by different reinsertionprocesses, at least some of which are executed at least partially inparallel with one another. Each reinsertion process may be identified bya respective process ID.

In such embodiments, when one of the reinsertion processes frees a slotby removing a node from the tree, the method may comprise allowing thefreed slot to be re-used to create a new node only by one of thereinsertion processes with a same process ID as the first reinsertionprocess.

That is, when a first one of the reinsertion processes processing one ofthe selected reinsertions frees a slot by removing the respective oldparent from the tree, the respective slot may be re-used to store a newentry representing a new node newly created by a further of the selectedreinsertions) in the same or a subsequent one of said iterations); andthe method may comprise allowing the freed slot to be re-used to createthe new node only by one of the reinsertion processes with a sameprocess ID as the first reinsertion process.

In embodiments, the method may allow no freed slot to be used to storean entry representing any node other than a newly created node createdby one of the reinsertion processes with the same process ID as thereinsertion process which freed the respective slot.

In embodiments, said one of the selected reinsertions processed by thefirst reinsertion process may comprise a reinsertion that leaves therespective old parent with only one remaining child, such that therespective old parent is removed from the tree and the remaining childbecomes the child of the old parent's parent.

In embodiments, the further selected reinsertion may leave itsrespective old parent with at least two children, and create the newlycreated node as the respective new parent in order to accommodate theinput node as a sibling of another, target node in the tree.

In embodiments, the processor comprises a buffer comprising a pluralityof slots, each respective one of the nodes in the tree being representedas an entry in a respective one of the plurality of slots; wherein thegraphics processor is configured to run a plurality of processesincluding at least some in parallel with one another, wherein eachprocess is configured to process a respective one or more of the inputnodes, each process being identified by a respective process ID; whereinin the updating step of at least one of the iterations, an old parent isremoved from the tree and the old parent's respective slot is freed forstoring a new entry representing a newly created node created by afurther one of the selected reinsertions; and none of the freed slots isallowed to be used to store an entry representing any node other than anewly created node created by a process with the same process ID as thatwhich freed the slot in the same or a preceding iteration.

In embodiments, the processing of each respective input node by therespective reinsertion process may comprise:

-   -   performing the search for the candidate reinsertions of the        respective input node,    -   performing the scoring of the candidate reinsertions of the        respective input node to determine the expected reduction in        computational cost, and    -   if any one of the candidate reinsertions of the respective input        node is selected as a respective one of the selected        reinsertions, performing an execution of the selected        reinsertion by performing the updating of the current BVH with        the respective selected reinsertion.

According to another aspect disclosed herein there may be provided aprocessor comprising logic configured to perform any of the methodsdisclosed herein.

In embodiments the processor may be embodied in hardware on anintegrated circuit.

According to another aspect, there is provided a method ofmanufacturing, using an integrated circuit manufacturing system, theprocessor of any embodiment disclosed herein.

According to another aspect, there is provided an integrated circuitdefinition dataset that, when processed in an integrated circuitmanufacturing system, configures the integrated circuit manufacturingsystem to manufacture the processor of any embodiment disclosed herein.

According to another aspect there is provided an integrated circuitmanufacturing system configured to manufacture the processor of anyembodiment disclosed herein.

In embodiments the method may comprise steps corresponding to theoperations of any embodiment disclosed herein.

According to another aspect there may be provided a graphics processingsystem configured to perform the method.

According to another aspect there is provided computer readable codeconfigured to cause the method to be performed when the code is run.

According to another aspect there is provided a computer readablestorage medium having encoded thereon the above-mentioned computerreadable code.

According to further aspects disclosed herein, there may be provided acorresponding method of operating the processor, and a correspondingcomputer program configured to operate the processor. According to yetfurther aspects there may be provided a corresponding method ofmanufacturing the processor, a corresponding manufacturing facilityarranged to manufacture the processor, and a corresponding circuitdesign data set embodied on computer-readable storage.

For instance according to one aspect there may be provided anon-transitory computer readable storage medium having stored thereon acomputer readable description of the processor of any embodiment hereinwhich, when processed in an integrated circuit manufacturing system,causes the integrated circuit manufacturing system to: process, using alayout processing system, the computer readable description of the logicor processor so as to generate a circuit layout description of anintegrated circuit embodying said logic or processor; and manufacture,using an integrated circuit generation system, the logic or processoraccording to the circuit layout description.

According to another aspect, there may be provided an integrated circuitmanufacturing system comprising: a non-transitory computer readablestorage medium having stored thereon a computer readable description ofthe processor of any embodiment disclosed herein; a layout processingsystem configured to process the computer readable description so as togenerate a circuit layout description of an integrated circuit embodyingsaid logic or processor; and an integrated circuit generation systemconfigured to manufacture the logic or processor according to thecircuit layout description.

According to another aspect there may be provided a method ofmanufacturing, using an integrated circuit manufacturing system, theprocessor of any embodiment disclosed herein, the method comprising:processing, using a layout processing system, a computer readabledescription of said circuit so as to generate a circuit layoutdescription of an integrated circuit embodying the logic or processor;and manufacturing, using an integrated circuit generation system, thelogic or processor according to the circuit layout description.

According to another aspect there may be provided a layout processingsystem configured to determine positional information for logicalcomponents of a circuit derived from the integrated circuit descriptionso as to generate a circuit layout description of an integrated circuitembodying the processor of any embodiment disclosed herein.

Other variants, implementations and/or applications of the disclosedtechniques may become apparent to a person skilled in the art once giventhe disclosure herein. The scope of the present disclosure is notlimited by the above-described embodiments but only by the followingclaims.

What is claimed is:
 1. A method performed by a graphics processor, themethod comprising: obtaining a starting bounding volume hierarchy (BVH),being a data structure comprising nodes representing different 3Dregions of space in a modelled environment, the data structurecomprising a tree in which the nodes are arranged hierarchically from aroot node down to a plurality of leaf nodes, wherein the region modelledby each leaf node encompasses at least one primitive or part of aprimitive; performing one or more iterations, starting with a firstiteration which starts with the starting BVH as a current BVH, eachiteration comprising: for each respective one of a plurality of inputnodes in the tree of the current BVH, searching for at least onerespective candidate reinsertion which would move the respective inputnode from an old parent to a new parent in the tree, and which comparedto the current BVH would reduce an expected computational cost ofsearching the tree to determine whether a modelled ray would intersectwith one of the primitives, according to a metric for estimating theexpected computational cost, and performing at least a first update toupdate the current BVH with one or more selected reinsertions selectedfrom among the candidate reinsertions; wherein in the search forcandidate reinsertions, either or both of: the new parent is limited tobeing related to the old parent by an ancestor at no more than apredetermined number of hierarchal levels above the old parent in thetree of the current BVH, and/or the input nodes are limited to being ator above a hierarchical level in the tree of the current BVH apredetermined number of hierarchical levels below the root node.
 2. Themethod of claim 1, wherein the new parent is limited to being related tothe old parent by an ancestor at no more than the predetermined numberof hierarchical levels above the old parent.
 3. The method of claim 1,wherein the input nodes are limited to being at or above a hierarchicallevel in the tree of the current BVH a predetermined number ofhierarchical levels below the root node.
 4. The method of claim 3,wherein the new parent is limited to being at or above a hierarchicallevel in the tree of the current BVH a predetermined number ofhierarchical levels below the root.
 5. The method of claim 1, wherein ineach of one, some or all of the candidate reinsertions, the candidatereinsertion increases a number of child nodes at the new parent or addsa new node to the data structure as the new parent.
 6. The method ofclaim 1, wherein said plurality of input nodes is only a subset of atotal number of nodes in the tree, the subset excluding the root node aswell as one or more internal nodes and/or leaf nodes.
 7. The method ofclaim 6, wherein in at least one layer in the tree of the current BVH,the subset comprises only every Nth node within that layer, wherein N isan integer greater than two.
 8. The method of claim 1, wherein for eachof said plurality of input nodes in any given one of said iterations,said search for at least one candidate reinsertion may or may notsuccessfully find at least one candidate reinsertion that would reducethe expected computational cost according to said metric.
 9. The methodof claim 1, wherein for each of said plurality of input nodes, saidsearched-for at least one candidate reinsertion consists of a singlebest candidate reinsertion, among multiple possible candidatereinsertions of the respective input node, that according to said metricwould give a greatest reduction in the expected computational cost. 10.The method of claim 1, wherein said searching for the candidatereinsertions comprises, for each respective one of the plurality ofinput nodes: scoring each of a respective plurality of possiblereinsertions for the respective input node according to said metric, todetermine a difference in the estimated computational cost that thereinsertion would achieve; and from among the multiple possiblereinsertions for the respective input node, identifying the reinsertionwhich gives the lowest difference, and if the lowest difference for therespective input node is a reduction in the expected computational cost,selecting the identified reinsertion as the at least one searched-forcandidate reinsertion for the respective input node.
 11. The method ofclaim 1, wherein the method comprises determining a starting score beinga score of the starting BVH according to said metric, wherein the firstiteration starts with the starting score as a current score of thecurrent BVH; and wherein each iteration comprises updating the currentscore to account for the first update.
 12. The method of claim 1,comprising after said one or more iterations, searching the tree of thecurrent BVH to determine whether a modelled ray intersects with any ofthe primitives.
 13. The method of claim 12, comprising outputtinggraphical data for controlling a screen to render a scene representingat least part of the modelled environment, including a lighting effectbased on the modelled ray.
 14. The method of claim 1, wherein in thesearching step of at least one of the iterations, the one or morecandidate reinsertions include at least one candidate reinsertion thatwould leave the old parent with more than one child, and/or give the newparent more than two children.
 15. The method of claim 1, wherein in theupdating step of at least one of the iterations, at least one of theselected reinsertions leaves the respective old parent with more thanone child, and/or gives the respective new parent more than twochildren.
 16. The method of claim 1, wherein the one or more selectedreinsertions selected for inclusion in the first update are selectedaccording to a conflict check to determine whether any group of thecandidate reinsertions would affect a same part of the tree of thecurrent BVH as one another, and if so selecting only one of the group toinclude in the first update.
 17. The method of claim 16, wherein saidone of the group selected for the first update is selected based onbeing the candidate reinsertion from among the group that gives thegreatest reduction in the expected computational cost according to saidmetric.
 18. The method of claim 16, wherein at least one of theiterations further comprises, after the first update, performing asecond update within the same iteration to update the current BVH withanother of said group.
 19. A non-transitory computer readable storagemedium having stored thereon computer executable code configured suchthat when run on a graphics processor causes said graphics processor toperform a method comprising: obtaining a starting bounding volumehierarchy (BVH), being a data structure comprising nodes representingdifferent 3D regions of space in a modelled environment, the datastructure comprising a tree in which the nodes are arrangedhierarchically from a root node down to a plurality of leaf nodes,wherein the region modelled by each leaf node encompasses at least oneprimitive or part of a primitive; performing one or more iterations,starting with a first iteration which starts with the starting BVH as acurrent BVH, each iteration comprising: for each respective one of aplurality of input nodes in the tree of the current BVH, searching forat least one respective candidate reinsertion which would move therespective input node from an old parent to a new parent in the tree,and which compared to the current BVH would reduce an expectedcomputational cost of searching the tree to determine whether a modelledray would intersect with one of the primitives, according to a metricfor estimating the expected computational cost; and performing at leasta first update to update the current BVH with one or more selectedreinsertions selected from among the candidate reinsertions; wherein inthe search for candidate reinsertions, either or both of: the new parentis limited to being related to the old parent by an ancestor at no morethan a predetermined number of hierarchal levels above the old parent inthe tree of the current BVH, and/or the input nodes are limited to beingat or above a hierarchical level in the tree of the current BVH apredetermined number of hierarchical levels below the root node.
 20. Agraphics processor comprising: memory comprising one or more memoryunits, and processing apparatus comprising one or more execution units;wherein the memory stores code arranged to run on the processingapparatus, the code being configured when run to cause the processingapparatus to perform a method comprising: obtaining a starting boundingvolume hierarchy (BVH), being a data structure comprising nodesrepresenting different 3D regions of space in a modelled environment,the data structure comprising a tree in which the nodes are arrangedhierarchically from a root node down to a plurality of leaf nodes,wherein the region modelled by each leaf node encompasses at least oneprimitive or part of a primitive; performing one or more iterations,starting with a first iteration which starts with the starting BVH as acurrent BVH, each iteration comprising: for each respective one of aplurality of input nodes in the tree of the current BVH, searching forat least one respective candidate reinsertion which would move therespective input node from an old parent to a new parent in the tree,and which compared to the current BVH would reduce an expectedcomputational cost of searching the tree to determine whether a modelledray would intersect with one of the primitives, according to a metricfor estimating the expected computational cost; and performing at leasta first update to update the current BVH with one or more selectedreinsertions selected from among the candidate reinsertions; wherein inthe search for candidate reinsertions, either or both of: the new parentis limited to being related to the old parent by an ancestor at no morethan a predetermined number of hierarchal levels above the old parent inthe tree of the current BVH, and/or the input nodes are limited to beingat or above a hierarchical level in the tree of the current BVH apredetermined number of hierarchical levels below the root node.